How to create subfolders while using FOR /F in a Batch file - windows

I want to process a folder and files in sub folders and after processing want to move files to new location (folder).
Current command is working but it is not creating sub folders and generating all new files in same folder (i.e. output folder)
for /F %%i in (filelist.txt) do (process.exe %%i > output\%%~nxi)
I need it to save it in same folder structure, as its source folder.
Where filelist.txt (source folder) is:
c:\backup\oldwork\browse.asp
c:\backup\oldwork\capital.asp
c:\backup\oldwork\make.asp
c:\backup\oldwork\conf\config.asp
c:\backup\oldwork\conf\global.asp
and I want my script to generate output (destination folder) like:
c:\backup\output\browse.asp
c:\backup\output\capital.asp
c:\backup\output\make.asp
c:\backup\output\conf\config.asp
c:\backup\output\conf\global.asp
Currently above For /F command is generating output like:
c:\backup\output\browse.asp
c:\backup\output\capital.asp
c:\backup\output\make.asp
**c:\backup\output\config.asp** (not following directory structure)
**c:\backup\output\global.asp**

The reason your code doesn't work is to do with what your outputting:
output\%%~nxi
Where if you check your documentation you would see:
You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string
The modifiers can be combined to get compound results:
%~nxI - expands %I to a file name and extension only
Since you are only expanding to name and extension, the folder structure is ignored.
Solution:
What I would do in your situation is this, change your filelist.txt to this format:
browse.asp
capital.asp
make.asp
conf\config.asp
conf\global.asp
And then change your for-loop as such:
for /F %%i in (filelist.txt) do (process.exe c:\backup\oldwork\%%i > output\%%i)
Which should work out for you.

for /F %%i in (filelist.txt) do (md "output%%~pi"&process.exe %%i > "output%%~pnxi")
should do the task, creating output\backup\oldwork\browse.asp etc. (for lack of adequate description of required result)

Related

issue Rename files in sub directories

I'm trying to rename the below the name of files.
as you can see the some files have ".DSD".
So I want to remove these ".DSD"
I was just refering other ansering from here such as
for /r %x in (*.DSD) do ren "%x" *.dad
But it does not work so
what am i supposed to do this ?
before after
DDS1_150223.cpj.DSD DDS1_150223.cpj
DDS1_150211.cpj.DSD DDS1_150211.cpj
72 ranndom value.xls 72 ranndom value.xls
FREQUENCY_AGILE_MEM.mif.DSD FREQUENCY_AGILE_MEM.mif
.... ...
This command will work:
for /r %a in (*.DSD) do #move "%a" "%~pa%~na"
The command above make use of some optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string

CMD error filename/directory error

I used to program batch files for work but I quit since a long time, now I'm back on the job and there seemed to be a bit of a problem.
I try to edit txt files using CMD commands on a batch file:
e.g. echo hello >> *.txt
the thing is I want to add the text to all the txt files in that directory and I remember the * represented all the files in that directory with the same extension unless it's used as *.* then it includes all the files, but now all it does is just writes this error on cmd:
The filename, directory name, or volume label syntax is incorrect.
Can anyone can give a little help?
You can use a FOR /F loop with a DIR command to iterate the full paths and pass those over to the redirection append >> to echo to the text files accordingly.
Example FOR Loop
Be sure to change the value of the Folder= variable to be the directory you need to append to the files with the ECHO command.
Confirmed working batch script example
#ECHO ON
SET Folder=C:\MyFolder
CD /D "%Folder%"
FOR /F "TOKENS=*" %%A IN ('DIR /B /A-D "%Folder%\*.txt"') DO ECHO HELLO>>%%~fA
PAUSE
EXIT
Further Resources
DIR
FOR /F
In addition, substitution of FOR variable references has been
enhanced. You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string

Simple Windows batch to move folders

I'm simply trying to move all files and subdirectories inside d:\temp\test to d:\temp\archive and so I tried these commands:
move d:\temp\test\* d:\temp\archive\
move d:\temp\test\*.* d:\temp\archive\
but I got this error in return:
The filename, directory name, or volume label syntax is incorrect.
Then I dug around on the web and tried this inside a doc bat:
for %%F in ( d:\temp\test\*.* ) do move /Y %%F d:\temp\archive
and this time it shows no error, but everything is standing still and made no changes.
What am i missing here? I'm trying this on Windows 10.
ok, if you want to just move all the files directories from inside \test\ then this will do the files first, then directories in a batch. The for /d will copy the directories and sub directories and files.
#echo off
move "d:\temp\test\*" "d:\temp\archive"
for /d %%a in ("D:\temp\test\*") do move "%%~fa" "d:\temp\archive\"
as a side note, from cmd when running below you get an error.
move d:\temp\test\* d:\temp\archive
That is because it will move all files, but not directories. If you get The filename, directory name, or volume label syntax is incorrect. there are no files and only folders which your move command cannot see.
NOTE from within a batch file, the /Y switch is disabled and folders will not be replaced if exist. So if you plan on overwriting often, perhaps rather use xcopy and update archive instead, then run a delete in d:\temp once the files have successfully copied.
Lastly, always enclose your paths in double ". In this case it will work fine without the double quotes, but if you have something like move d:\program files\temp\* d:\temp\archives it will create an error because of the space between program and files so it is always better to use move "d:\program files\temp\*" "d:\temp\archive
EDIT Understanding the %%~ assignments. In these examples we use %%I instead of %%a
%~I : expands %I removing any surrounding quotes (")
%~fI : expands %I to a fully qualified path name
%~dI : expands %I to a drive letter only
%~pI : expands %I to a path only
%~nI : expands %I to a file name only
%~xI : expands %I to a file extension only
%~sI : expanded path contains short names only
%~aI : expands %I to file attributes of file
%~tI : expands %I to date/time of file
%~zI : expands %I to size of file
%~$PATH:I : searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
if the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string
The modifiers can be combined to get compound results:
%~dpI : expands %I to a drive letter and path only
%~nxI : expands %I to a file name and extension only
%~fsI : expands %I to a full path name with short names only
%~dp$PATH:I : searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI : expands %I to a DIR like output line`

How to get only file name in 'for' batch command and not file extension

When using this code to upload video's to YouTube, the title that gets named has the file extension at the end (in this case .mp4)
for %%f in (*.mp4) do python --file="%%f" --title="%%f"
use for %%f in (*.csv) do echo "%%~nf"
for more info do a for /? .. this is the part that you would liek to see
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string

Filename without Extension in Batch Script

for %%a in (.\*.jpg) do
The code above will store every jpg picture's name in %%a, but it stores the full name with the file extension, for example "Q.jpg".
I'm using a cmd utility for resizing images,
resize /width:100 %%a %%a.jpg
It will resize "Q.jpg" and then name it to "Q.jpg.jpg", as you can see the extension is now a part of file name!!!
I want to avoid it.
Simple, type for /? for more info:
To only get filename:
for %%a in (.\*.jpg) do resize /width:100 %%a %%~na.jpg
Quoting Windows batch help (type for /?):
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string
The modifiers can be combined to get compound results:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line
And that should help you with any further problems.

Resources