I've been trying to move some files into another folder, based on the file extension. Only problem is that the filenames have spaces in them.
In cmd prompt to move a single file I guess it would look like:
D:\move "D:\folder1\140227 file.xls" D:\folder2
In a bat file I've got:
for /R in D:\folder1 %%f (*.xls) do move %%f D:\folder2\
But obviously that doesn't work due to the lack of quotation marks...
for /R "D:\folder1" %%f in (*.xls) do move "%%f" "D:\folder2\"
..so put the quotes in...
(in keyword misplaced)
Related
I have thousands of files to move.
I have already used a batch file to create the directories I need.
My file names look like this:
6711_05_12.pdf
10504_06_15.pdf
559_07_11.pdf
The first characters up to the "_" are the directory the files need to go into. Started the batch file - but don't know how to identify the file name.
#echo off
setlocal EnableDelayedExpansion
for %%I in (*.pdf) do (
xcopy ???
)
Is there a manual for batch files?
You don't even need a batch file. The following one line command will do the trick.
for %I in (*.pdf) do #for /f "eol=_ delims=_" %A in ("%I") do #copy "%I" "%A"
Simply double up the percents if you want to put the command in a batch file.
I had a batch script that rename files inside the folder which looked like this:
ren B:\Backups\*.bc_ *.bc
Now I have files between many folders, the back up creates a new folder with a new name every day, and i need to rename files across several folders.
How can I do it? How to correctly use wild card in this case?
You cannot use a wildcard in the path in your REN statement. You will have to use some form of the FOR command.
Suppose you want to rename all *.bc_ files in the entire folder hierarchy rooted at B:\Backups.
You could use FOR /R to iterate all .bc_ files within the hierarchy and rename each file individually.
for /r "B:\Backups" %%F in (*.bc_) do ren "%%F" "%%~nF.bc"
Or you could use FOR /D /R to iterate all the folders under the root and run your wildcard REN against each of the folders
for /d /r "B:\Backups" %%F in (.) do ren "%%F\*.bc_" *.bc
Both of the commands above are designed to be used in a batch script. Change each double percent into a single percent if you want to run the command from the command line instead of from within a batch file.
My requirement is to write a batch script that will compare the files in two folders. If a file exists in both SourceFolder and TargetFolder, then overwrite the file in TargetFolder with the file in SourceFolder.
Using a for-statement and an if-statement I can achieve this:
for /R %Source% %%G in (Prefix.*.ext) do (
if exist %Target%%%~nxG (
del %%G
copy %Target%%%~nxG %Source%
)
)
Although an additional requirement is to only copy files that start with 'prefix.' and end in '.ext' and also to exclude all files that contain the word 'exclude'.
In English: Copy all files from that source folder that start with 'Prefix.', end in '.ext', does not contain the text 'exclude'. and already exists in the target folder.
This is where I get stuck. Does anyone know how to do this in batch?
You can use xcopy for this. First, I am assuming that Prefix and ext are actual strings, to use variables instead you would have to wrap them like %Prefix%.
Second, you will have to make a new text file. Name it excludes.txt and put it in the same directory as your batch file. (If you don't want to make a batch file, then just put it in the directory that is active when you run the command). The only contents of this file should be your EXCLUDE string with no quotes, or other markup.
Ok, the command itself:
xcopy %Source%\Prefix.*.ext %Target% /U /EXCLUDE:excludes.txt
To break it down:
%Source%\Prefix.*.ext Selects the files in the source folder that start with Prefix and end with .ext
%Target% Specifies the destination for the files
/U Only copy files that already exist in the target directory
/EXCLUDE:excludes.txt This will read in excludes.txt and exclude any file that matches any part of the excludes.txt file.
That's it! This is probably easier than writing a FOR statement with a nested IF.
After reading this SO question, I ended up doing it like this. (Before the question got answered)
pushd %Target%
attrib +h *Exclude
for /R %%G in (Prefix.*.ext) do (
if exist %Target%%%~nxG (
del %%G
copy %Target%%%~nxG
)
)
attrib -h *Exclude
popd
The xcopy solution probably looks better although I'd prefer not to have to create (and remove) files if I can help it.
I'm trying to automate a little tedious process I have to go through while updating files on my server. I have a content folder, with a lot of subfolders, each potentially containing some files. Some files have a compressed version (ending with .bz2). So a folder could have something like:
sound1.wav
sound1.wav.bz2
sound2.wav
texture1.tex
texture2.tex
texture2.tex.bz2
What I want to do is remove every file (somewhere in the content folder) that has an equivalent compressed file. Meaning in the above example I just want 'texture2.tex' and 'sound1.wav' removed.
for /r %%f in (*) do if exist "%%f.bz2" del "%%f"
Or, at the command line instead of in a batch file:
for /r %f in (*) do if exist "%f.bz2" del "%f"
Little mistake. It should be:
for /r %%f in (*.bz2) do if exist "%%f" del "%%f"
Or, at the command line instead of in a batch file:
for /r %f in (*.bz2) do if exist "%f" del "%f"
I am using the following batch file to make a zip file for each xml in a folder:
FOR %%f in ("C:\files\*.xml") DO 7za.exe a C:\files\zips\%%~nf.zip (%%f)
However if the file name has a space in it (test plop.xml) then the batch file does not work. It seems to split the name and thinks it is 2 files.
How to modify the batch file so that it properly handles file names with spaces?
Try placing quotes around the output file name.
Change
FOR %%f in ("C:\files*.xml") DO 7za.exe a C:\files\zips\%%~nf.zip (%%f)
to:
FOR %%f in ("C:\files*.xml") DO 7za.exe a "C:\files\zips\%%~nf.zip" (%%f)
May also be the variable %%f, may need to place quotes around this as well.