Saving multiple files through xcopy in a batch file - 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

Related

Copying a Folder and renaming it using command prompt

I am trying to copy a folder and paste it in the same directory it was copied from.
For example
C:\Test is the main directory which consists of a folder ACDM, I would like to copy ACDM in the same directory and rename the new folder to ACDM1 which will have all the same files as ACDM has
I would like to do it using command prompt
I tried the following
C:>Xcopy C:\Test C:\Test\ACDM1 /E /U
Cannot perform a cyclic copy
0 File(s) copied
which fails, not sure hoe to add REN command with XCOPY command.
Need help ASAP as i would want to create a batch file which will create a copy of an existing folder and rename it according to a name retrieved from a text file..
xcopy "C:\Test\ACDM\*.*" "C:\Test\ACDM1\" /s/h/e/k/f/c
for /f "delims=" %%a in (yourtextfilename) do xcopy "C:\Test\ACDM" "C:\Test\%%a\" /E
as a .bat file line. Directly from the prompt, change each %% to %
I've assumed (for lack of futher information) that your textfile contains just the one line
ACDM1
neither do you specify the textfilename tou want to use.
xcopy C:\Test\ACDM C:\Test\ACDM1\ /E /Q

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 to delete files that have their paths in a txt file

I need a batch that will delete files from a LAN, all the paths of the files being saved in a txt file. Don't know how the batch will "read" the paths then delete those files with DEL command.
The line that works so far is:
del "path\*.txt" - for deletion of all txt in some folder (path being the actual line, like c:\folder\folder\*.txt), but I need for a lot more paths.
i pushed then the batch with psexec.exe (for the LAN deletion)
I guess it's 2-3 lines of code, but I'm new to batching & scripting, could someone pls help! Thanks in advance
You can use the FOR /F command to process each line in your input file. Here is a SO answer: How do you loop through each line in a text file using a windows batch file?
This just worked for me (the parentheses around the file name are necessary):
for /F "tokens=*" %%A in (myfile.txt) do del "%%A"
myfile.txt looks like this:
a.txt
b.txt
a b c.txt
del /s *.txt
/s means - delete from all subfolders..
or to iterate over directories:
for /d %i in (*.*) do del %i\*.exe
!!!) you should escape % with % if your code is in batch file

batch script - run command on each file in directory

I need to convert some xls files into xlsx files. I can successfully convert one xls file into xlsx by running this command into cmd prompt (windows):
ssconvert inputFileName.xls outputFileName.xlsx
(ssconvert is a Gnumeric's command-line utility that can convert between different spreadsheet file formats)
I'd like to write a batch file that FOR EACH file in a specified directory runs the command I wrote above, using the current file name both for input and for output filename.
For example, if I have this set of files:
c:\directory\file1.xls
c:\directory\file2.xls
c:\directory\file3.xls
the output should be
c:\directory\file1.xlsx
c:\directory\file2.xlsx
c:\directory\file3.xlsx
so the batch pseudo code should be something like
directory = c:\directory\
for (fileName in directory)
ssconvert fileName.xls fileName.xlsx
Can anyone help me?
for /r %%v in (*.xls) do ssconvert "%%v" "%%vx"
a couple have people have asked me to explain this, so:
Part 1: for /r %%v in (*.xls)
This part returns an array of files in the current directory that have the xls extension. The %% may look a little curious. This is basically the special % character from command line as used in %PATH% or %TEMP%. To use it in a batch file we need to escape it like so: %%PATH%% or %%TEMP%%. In this case we are simply escaping the temporary variable v, which will hold our array of filenames.
We are using the /r switch to search for files recursively, so any matching files in child folders will also be located.
Part 2: do ssconvert "%%v" "%%vx"
This second part is what will get executed once per matching filename, so if the following files were present in the current folder:
c:\temp\mySheet.xls,
c:\temp\mySheet_yesterday.xls,
c:\temp\mySheet_20160902.xls
the following commands would be executed:
ssconvert "c:\temp\mySheet.xls" "c:\temp\mySheet.xlsx"
ssconvert "c:\temp\mySheet_yesterday.xls" "c:\temp\mySheet_yesterday.xlsx"
ssconvert "c:\temp\mySheet_20160902.xls" "c:\temp\mySheet_20160902.xlsx"
Actually this is pretty easy since Windows Vista. Microsoft added the command FORFILES
in your case
forfiles /p c:\directory /m *.xls /c "cmd /c ssconvert #file #fname.xlsx"
the only weird thing with this command is that forfiles automatically adds double quotes around #file and #fname. but it should work anyway
you can run something like this (paste the code bellow in a .bat, or if you want it to run interractively replace the %% by % :
for %%i in (c:\directory\*.xls) do ssconvert %%i %%i.xlsx
If you can run powershell it will be :
Get-ChildItem -Path c:\directory -filter *.xls | foreach {ssconvert $($_.FullName) $($_.baseName).xlsx }
I am doing similar thing to compile all the c files in a directory.
for iterating files in different directory try this.
set codedirectory=C:\Users\code
for /r %codedirectory% %%i in (*.c) do
( some GCC commands )

Read a file and do a copy

The following script runs fine at CMD prompt
FOR /F "delims=" %R IN (C:\Test01.txt) DO IF Ready == %R COPY /y C:\Test01.txt "D:\Ready.txt"
File is copied succesfully to D drive if the text01 file as 'Ready' as one of the line,
But create a BAT file with the same script its failing with the following error
R was unexpected at this time.
Any idea to fix the bat file?
You need to use double % (e.g. %%R) notation in bat files for variables.
Correct command is,
FOR /F "delims=" %%R IN (C:\Test01.txt) DO IF Ready == %%R COPY /y C:\Test01.txt "D:\Ready.txt"
For Syntax
NOTE: %R can be used only in command line. In a batch file, variables have to be %%R.
Please read this for more info http://www.robvanderwoude.com/for.php

Resources