简体   繁体   English

批量变量循环查询

[英]Batch Variable Loop Inquiry

I'm trying to create a batch to first off grab all folder names in a directory and output those to a txt file. 我正在尝试创建一个批处理,以首先抓取目录中的所有文件夹名称并将其输出到txt文件。

dir "C:\directory1\directory2\directory1\work" > C:\output.txt /b /o

Then, run a program specific backup function that requires a folder name to match the outputs of above. 然后,运行特定于程序的备份功能,该功能需要一个文件夹名称才能与上述输出匹配。 I can send it through a loop but I need the names to be used as variables. 我可以通过循环发送它,但是我需要将名称用作变量。 Example: there's a folder names "Test_Test1" and another named "Test2_Test3" that I backup using the output above. 示例:我使用上面的输出备份了一个名为“ Test_Test1”的文件夹和另一个名为“ Test2_Test3”的文件夹。 I then need to run a batch command which would look like the statement below if running it against a single line 然后,我需要运行一个批处理命令,如果对一行执行它,它将类似于下面的语句

staging-backup Test_Test1 C:\BackupLocation\Test_Test1

I want to automate this process by grabbing all folder names, throwing them in to a file(if necessary) and then running the above command on all lines but updating the Test_Test1 in both the name (reference location) and the backup location folder(which will be created). 我想通过抓取所有文件夹名称,将它们放入文件中(如有必要),然后在所有行上运行上述命令,但同时更新名称(参考位置)和备份位置文件夹(其中的Test_Test1)来自动化此过程将被创建)。

I got as far as using DelayedExpansion to create the variables but hit a head scratcher as I couldnt wrap my brain around how to grab these variables and input them via another loop to run the staging-backup portion of the batch. 我可以使用DelayedExpansion来创建变量,但由于无法抓住这些变量并通过另一个循环输入它们以运行批处理的暂存部分而无法解决问题,因此遇到了麻烦。

For reference, below is my DelayedExpansion setup @echo off set "file=C:\\output.txt" 供参考,以下是我的DelayedExpansion设置@echo off set“ file = C:\\ output.txt”

setlocal EnableDelayedExpansion
<"!file!" (
  for /f %%i in ('type "!file!" ^| find /c /v ""') do set /a n=%%i && for /l     %%j in (1 1 %%i) do (
    set /p "line_%%j="
  )
)

echo Number of lines: !n! 
echo.
echo !n! > c:\lines.txt
echo Line contents:
for /l %%i in (1 1 !n!) do echo(!line_%%i!
echo.

pause

Any ideas on how best to tackle that? 关于如何最好地解决这个问题的任何想法? I'm combining these in to 1 batch if possible so I'd need the variables to stick around momentarily while the work is being done. 如果可能的话,我会将它们组合为1个批处理,因此我需要变量在工作完成时暂时停留。 I guess I'm just struggling to figure out how to call these variables in a variable for the staging-backup command that I'm using. 我想我只是想弄清楚如何在我正在使用的staging-backup命令的变量中调用这些变量。

An idea : 一个主意 :

@Echo off&cls
setlocal enabledelayedexpansion
set /a $count=1
for /f "delims=" %%a in ('dir "C:\directory1\directory2\directory1\work" /b/o') do (
  md "C:\BackupLocation\%%a-!$Count!" 2>nul
  staging-backup "%%a-!$Count!" "C:\BackupLocation\%%a-!$Count!"
  set /a $Count+=1
)

Thanks for the info! 谢谢(你的)信息! I made soem edits as the -!$Count! 我将soem编辑为-!$ Count! was adding -1, -2 etc to the folders which I decided against doing as the folders are unique names anyways. 我将-1,-2等添加到文件夹中,我决定不这样做,因为文件夹无论如何都是唯一的名称。 I also found that I needed to change the formatting a tad and ended with this below: 我还发现我需要更改tad的格式,并在此结尾:

md "C:\BackupLocation\
setlocal enabledelayedexpansion
set /a $count=1
cd /d C:\staging-backup\location
for /f "delims=" %%a in ('dir "C:\directory1\directory2\directory1\work"     /b/o') do (
staging-backup "%%a" "C:\BackupLocation\%%a"
cd /d C:\staging-backup\location
set /a $Count+=1
)

I've got some additional changes to make as I'm going to add in some logging and such but this was my main hurdle. 我要进行一些其他更改,因为要添加一些日志记录,所以这是我的主要障碍。 Thanks for the help! 谢谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM