1.

Solve : variables in FOR command?

Answer»

When using the for command in a batch,

e.g.

Code: [Select]
for /r %%g in (*.*) do ( ..........



Is there a WAY to make %%g relative rather than an absolute?

I need to use the variable %%g later but just with the filenames, not the absolute path including C:\dir1\dir2 etc.

[This is actually for a GPG decryption; it wants me to specify the output FILE for each decryption, not merely the directory; clearly it won't work if the output file points to a file that already exists!]


Many thanks for your help, possibly in stating the obvious
Look at the help for FOR

you can do this
for /r %%g in (*.*) do MyCommand.Exe %%~nxg
which will extract only the name

where you were PROBABLY doing
for /r %%g in (*.*) do MyCommand.Exe %%g

which will GIVE you the full path
Quote from: gpl on January 05, 2010, 05:56:27 AM

Look at the help for FOR

you can do this
for /r %%g in (*.*) do MyCommand.Exe %%~nxg
which will extract only the name

Excellent answer. Technically, ~n gets the name, ~x gets the extension (which includes the dot).

In the filename "Datalist.txt", Datalist is the name, and .txt is the extension.
Hi

Thanks very much for your quick reply.

I now have this:

for /r %%g in (*.*) do (
echo %pass%|gpg --batch -q --passphrase-fd 0 -o "D:\gpgD\%%~pnxg" --decrypt "%%g"
)


I want to recreate the file structure (using %%~pnxg) but in an existing directory D:\gpgD

This seems to work except for the fact it tries to write to the directory with double forward slashes
e.g. D:\\gpgD\\dir1\\file.gpg

Argh!

Thank you for your patience Quote from: impaled on January 05, 2010, 08:25:22 AM

This seems to work except for the fact it tries to write to the directory with double forward slashes
e.g. D:\\gpgD\\dir1\\file.gpg


... and this is a problem?
Sorry, you're right. \\ doesn't matter
Misdiagnosed the problem --- GPG won't output to a directory that doesn't yet exist (it won't create them itself, for some reason.)

I have solved it like this: output them in the current dir without the .gpg extensions, then robocopy out EXCLUDING .gpg extensions....

for /r %%g in (*.*) do (
echo %pass%|gpg --batch -q --passphrase-fd 0 -o "D:\gpgD\%%~dpng" --decrypt "%%g"
)

Thanks all for your help.Quote
"D:\gpgD\%%~dpng"

That's going to give you a very strange looking path...
More mistakes, on a roll today.

-o "%%~dpng"

Thanks!


Discussion

No Comment Found