简体   繁体   中英

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.

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. 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).

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.

For reference, below is my DelayedExpansion setup @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. 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.

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! was adding -1, -2 etc to the folders which I decided against doing as the folders are unique names anyways. I also found that I needed to change the formatting a tad and ended with this below:

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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