Opening folders containing a string in Batch?

November 11, 2009 at 15:32:57
Specs: Windows 7
Ok, I have a folder which contains a lot of files, each starting with a unique, 2-digit number, followed by their name. My code contains:

set /P FILENAME=What is the number?:

for %%F in (%FILENAME%\*.tga) do ECHO file 0 = %%F > textfile.tcs

However, this only works if the folders entire name is specified, not just the first two digits (which is what I'd prefer), I tried specifying an unknown with %FILENAME%*, but it didn't work (as you can see, I'm new to this).

Thanks in advance, Milun.


See More: Opening folders containing a string in Batch?

Report •


#1
November 11, 2009 at 16:00:11
this might work:

set /P FILENAME=What is the number?:

for /d %%F in (%FILENAME%*) do (
for %%G in (%%F\*.tga) do ECHO file 0 = %%G > textfile.tcs)

if you want the pathname stripped off, subsitute:
ECHO FILE 0 = %%~nxG
in code.


Report •

#2
November 11, 2009 at 17:06:09
Thanks, but it doesn't work. If I enter the full folder name, then it does, but otherwise it returns "The system could not find the file path specified".

Report •

#3
November 11, 2009 at 17:32:11
it worked on my end. only thing i can think, did you put the asterisk in here:
for /d %%F in (%FILENAME%*) do (
^ asterisk here in above line...

try putting an echo on very next line after the line above:
echo %%F %FILENAME%*
my understanding, your dir.s look like:
88zzz
99abc
61cpu
and these contain files with .tga extensions, in other words,
I interpreted the following statement:
"Ok, I have a folder which contains a lot of files, each starting with a unique, 2-digit"
as actually meaning:
"...folder which contains a lot of subfolders, each..."

sorry i couldn't help you more.


Report •

Related Solutions

#4
November 11, 2009 at 18:29:05
You interpretted my folders correctly, but I'm not entirely sure what you mean by your asterixes.

Report •

#5
November 11, 2009 at 19:25:27
only that %FILENAME% must have asterisk in line2 of code:
for /d %%F in (%FILENAME%*) do (

cause that was the only thing i could think of might be wrong, from what you reported about having to put full dir.name instead of just 2 digis. the asterisk is the wildcard that tells it to select ALL filenames beginning with the 2 digits specified by %FILENAME%


Report •

#6
November 11, 2009 at 19:59:57
Thanks for all your help. I did some tampering with my other code and it worked... to some extent. It seems to work, but always returns the error that the file specified couldn't be found. Is there a way to dissable that, because it works perfectly?

Report •

#7
November 11, 2009 at 20:42:07
Sorry for the double post, but It wasn't your codes fault for the error. some of my previous code accidentally got altered when I made room for yours. Thank you very much for your help!

Report •

#8
November 11, 2009 at 22:14:29
no problem, sorry for my klutzy code.
as for the error, I personally do not like running code that kicks an error unless i know exactly what the reason is, because of possibility of getting wrong information/results.
Rather than bury the error, I would put echos and pauses at critical junctures in the code to see what the system is seeing. also put delimiters around any items echoed so that leading / trailing spaces will show, like: echo [%FILENAME%]
as far as burying the error, if you know which command generates it, put: 2>nul after the command, which sends errors to device "nul" which is a black hole. good luck.

Report •

#9
November 11, 2009 at 22:41:47
It's ok, I've completely removed the error. Once again, thanks. You've been a big help.

Report •


Ask Question