Windows batch script nested for loop pass current filename from innerloop to another command

风流意气都作罢 提交于 2020-01-25 06:41:28

问题


Really struggling with double quotes, double percent signs etc in batch script.

So i have a folder, lets call it C:\EncryptedFiles It can have one or more subfolders, and each subfolder can have one or more encrypted file (with extension gpg).

I need to look at everything inside C:\EncyptedFiles folder and iterate over files in each subfolder, and decrypt those files in same place where encrypted file is there.

So if we have another folder called Subfolder1 with file EncryptedFile1.csv.gpg, its absolute path will be

C:\EncryptedFiles\Subfolder1\EncryptedFile1.csv.gpg

I need to take this file, run it through gpg command line and output a file

C:\EncryptedFiles\Subfolder1\EncryptedFile1.csv.gpg

So this is what i have so far -

Set ROOTDIR=C:\EncryptedFiles
for /f %%g in ('dir /b /s ""%ROOTDIR""') do (
      for /r %%i in (%%g) do (
         REM this is where i need to pass each file to gpg 
      )
)

I know a command to decrypt files looks like this -

gpg --batch --yes --passphrase myPassword --output "D:\testOutput.csv" --decrypt "D:\testOutput.csv.gpg"

Above command will take D:\testOutput.csv.gpg and decrypt it, and create an output file testOutput.csv

I need to put these two together, and i can't seem to get it working. For decryption, i am able to decrypt files by hardcoding their full path in double quotes, as shown above.

Now in the nested for loop, in outer loop, %%g is each subfolder within C:\EncryptedFiles, and it holds full path of that folder (which is what /s is for). In the inner loop %%i will have full path of each file within the subfolder.

Within Inner for loop i tried this -

setlocal EnableDelayedExpansion
setoutputName=%g%\%%~ni
setinputName=%%i
gpg --batch --yes --passphrase mypassword --output "%setoutputName%" --decrypt "%setinputName%"
endlocal

so the complete code is something like this -

    Set ROOTDIR=C:\EncryptedFiles
    for /f %%g in ('dir /b /s ""%ROOTDIR""') do (
          for /r %%i in (%%g) do (
             setlocal EnableDelayedExpansion
             setoutputName=%g%\%%~ni
             setinputName=%%i
             gpg --batch --yes --passphrase mypassword --output "%setoutputName%" --decrypt "%setinputName%"
            endlocal
          )
    )

My understanding behind setoutputName=%g%\%%~ni is that %g will give the directory name (from outer loop) and %%~ni will give input file's name without the extension. So .gpg will be removed. So output path will be same as path of current file being iterated over in the innerloop, with extension removed.

My batch script just hangs forever until i force quit it. Can someone please help me out. And hopefully, provide answers for batch script and not the command prompt. I know everyone says the only difference is that we use double percent instead of single percent when it comes to batch script, but i have run into scenrioa where behavior changes from command prompt to batch script


回答1:


I suppose one loop would suffice:

@echo off
set "ROOTDIR=C:\EncryptedFiles"
for /R %ROOTDIR% %%i in (*.gpg) do (
     gpg --batch --yes --passphrase mypassword --output "%%~dpni" --decrypt "%%~i"
)
pause


来源:https://stackoverflow.com/questions/58925280/windows-batch-script-nested-for-loop-pass-current-filename-from-innerloop-to-ano

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!