Iterating through a folder using batch script - batch-file

I have a folder containing 1000+ xml files. I need to modify these xml files, for which I am using xslt.
Now the problem that I am facing is that I want to use batch script to do this modification recursively for all the xml files in the folder, rather than doing it manually. How can I do it using batch script?
It would be helpful if anybody could tell me how can I read all the xml files present in a folder and copy them to another folder with the same name.

Transformation:
for /r c:\your_root_folder\ %f in (*.xml) do your_transform_command %f
Copy:
copy *.xml c:\your_target_folder\.

Assuming you are using DOS batch ...
A simple copy operation will work:
prompt> copy *.xml destinationDir
To loop and process files individually, we use:
for %%R in (*) do (
...
)

read this
HELP XCOPY,
and this
HELP FOR.
and try this
XCOPY \source\*.xml \destination /S
and try this
FOR %a IN (\source\*.xml) DO echo %a
and now read
HELP CALL
and read
HELP SET
and try this
FOR %a in (\source\*.xml) DO CALL youraction %~na
and by the time you understand what happened you are ready to achieve your goal.

Related

batch file which looks for specific files

I'm wondering if a batch file could help to move all the files which i need from a couple of folders to a specific one. The folder structure looks like this example below:
d:\home\\(random)\upload\\*.*
d:\home\\(random)\uplaod\\*.*
The files in the directory upload should be moved to a specific folder. It can be done by putting every single path in to the batch file, (there are more then 100 folders and there will some more in the future). I guess there is an easier way to get this done.
Thanks for you help in advance.
for /d %%a in ("D:\home\*") do echo move "%%a\upload\*" "x:\destination\
This is the syntax for use in a batchfile. For use on commandline replace every %%a with %a.
The for /d returns subfolders of D:\home\. Just append the upload\* part.

Batch File how to run all files in folder

I am trying to find a batch file command that will allow me to run all .lnk or .html files with in a folder. I tried using the wildcards (*.lnk) but this wouldn't work. Any ideas?
Based upon further comments, this should help - you don't need recursion to process a single folder, and this will handle long filenames.
#echo off
for %%v in ("C:\Users\username\Desktop\Test\*.lnk") do start "" "%%~v"
It will open every .lnk file in the folder.
You can use this:
for /r %%v in (*.lnk) do start %%v
Note:
Don't put spaces on shortcut filenames.

Batch Script to Rename JPEGS adding a 1 to the end

I have a large amount of JPEGs inside subfolders that I need to rename to their current name with an extra 1 at the end.
For example:
G:\FILENAME\Subfolder1\subfolder2\JPEGNAME.JPG
Want to rename to
G:\FILENAME\Subfolder1\subfolder2\JPEGNAME1.JPG
I have over 900 files in this format that I need to rename.
Any help?
edit I added /r as I see you have a tree of files to modify. Type this command in the main holding folder of the JPG files.
Here's a cmd prompt command. Delete the echo if you like what you see on the console.
for /r %a in (*.jpg) do echo rename "%a" "%~na1%~xa"
There may not be a need for anything more than a simple REN command. See How does the Windows RENAME command interpret wildcards?.
As long as none of your file names have multiple dots in the name, then the following should work for the current directory. Just make sure there are at least as many ? as the longest name.
ren *.jpg ??????????????????????????????????????1.jpg
Or to process the entire directory tree
for /r %F in (.) do #ren "%F\*.jpg" ??????????????????????????????????????1.jpg
Or you can iterate each file with a FOR or FOR /R loop and rename them one at a time as foxidrive does in his answer.
using renamer:
$ renamer --find '/(.*)\.JPG/' --replace '$11.JPG' *
to operate recursively, change the * at the end of of the command to **.

Comparing Folders and Subfolders files using batch command

I have two folders. I want to compare the files inside these folders.There are some subfolders in root folders. These sub folders are also containing some files. I want to compare these files in subfolder also. For that I am trying to write a batch file. Any kind of solution will help me a lot
I would not use a batch file at all to accomplish this task. BeyondCompare is here to do exactly that, and does it seemingly well.
Now on the other hand you really wanna do it via batch file, I'd suggest you install a tool called diff tools, and you'll be able to do something like:
diff.exe <file1> <file2> <htmlfile>
On the command line.
hope it helps
UPDATE
As a follow up to your comment, I write this, which also seems to work for me, and doesn't use any external tool. This is a simple example, but you can make it better.
if exist compare.log del compare.log
if exist missing.log del missing.log
for /f "delims=" %%a in ('dir/b/a-d c:\test\1') do (
if exist "C:\test\2\%%a" (
fc "c:\test\%%a" "C:\test1\%%a" >> compare.log
) else (
echo %%a is missing >> missing.log
)
)
Pause
try dir /b /s
to search for files within subdirectory, instead of dir /b

Saving multiple files through xcopy in a batch file

When I use the following code in command prompt, it works fine. But when I use the same code in a batch file, nothing happens. My batch file simply consists of:
for %f in (D:\flexcube1,D:\flexcube2,D:\flexcube3) do xcopy %f D:\o\ /e
but it does not work. I don't understand it and I have to use a batch file to copy multiple files. Any help would be really appreciated.
You need to use two % signs in for commands in a batch script (I know, who would have thought?)
for %%f in (D:\flexcube1,D:\flexcube2,D:\flexcube3) do xcopy %%f D:\o\ /e

Resources