I have below batch script to copy file from my computer to many computers.
#echo off
xcopy D:\some.txt \\10.124.66.72\texts
xcopy D:\some.txt \\10.294.66.46\testfolder
pause
In the script, i have mentioned all the other computer names/IPs. Now how can i keep other computer names in a separate text file and iterate them in batch file instead writing xcopy command many times? Or is it possible to mention list of computers, iterate through all and use single xcopy command?
Try this:
FOR /F "delims=" %%i IN (targets.txt) DO (
xcopy "D:\some.txt" "%%i"
)
targets.txt should contain entries like "\\10.124.66.72\texts" in each line
Related
I had a bash xcopy script, but got a lot of issues. After a lot of searching and reading I guess robocopy was a better tool to do the copy.
(The script must run on windows 10 computers without installing anything else and without internet access)
I'm trying to make a bash script that copy (with robocopy) some local network folders and files to a local custom directory. The aim is to be able to access to the files off from the local network.
The path to folders and files are stored inside a txt file (each line = a path)
I want to keep the structure of folder I save locally.
For example the folder X:\Path\to some\local\network\folder\with\some & characters\ will result in C:\PathTolocalFolder\Path\to some\local\network\folder\with\some & characters\ (without the X:\ letter)
Based on many similar questions (but not all at the same time) I have done this :
#echo off
SETLOCAL EnableDelayedExpansion
cls
cd C:
chcp 28591 > nul
for /f "delims=*" %%a in ('type "X:\path with spaces & specials characters\List.txt"') do (
REM echo %%a
REM echo !%%a!
echo %%a to C:\PathTolocalFolder!%%a!
ROBOCOPY "%%~a" "C:\PathTolocalFolder!%%a!" /S /A+:RA /R:1 /W:5
)
It is partially a success, but :
As there are special characters everywhere in paths and files names, I got some issues. Specially with & characters. My double quotes doesn't solve the problem. How could I go better?
For some cases, I want to save some files but not the whole directory where they are. The full path to the file is inside the text file. But as robocopy needs to add a space between folder path and file filter I have do some manipulation. How can I detect and extract the file name when there is one to adapt the robocopy command?
I want to use an exclusion list like I was doing before with xcopy. But robocopy doesn't accept a file in input for exclusions. I tried this to extract the exclusion file:
for /f "usebackq tokens*" %%D in ("C:\path to exclusion file\exclusions.txt") do (
if NOT "!dirs!"=="" (
Set dirs=!dirs! "%%D"
else (
Set dirs ="%%D"
)
)
But doesn't really know what I am doing and how to combine with the first part.
Bonus questions I'm using the robocopy log file functionality (removed from below) is there a way to archive (by adding the date in the name for example) previous log file before creating the new one? Is it possible to remove the progress percents in the log file but to display it in the terminal instead? How to use the "/np" option for log file but not for terminal display?
It's hard to me to understand how the delayed variables are working in batch files and how the different methods to read a file or variable are working.
Any help is welcome :)
Sorry for my bad English skills
thank for having read
For example, in a directory, I have N .eps files named name1.eps, name2.eps, ... and also much more pdf files. But within these pdf files, there are exactly N pdf files with the same filename as eps files, that is name1.pdf, name2.pdf,....
So I want a batch that can scan the current dir, give a list of all eps files and corresponding list of pdf files.
finally I want another batch named convert.bat which takes two parameters to run a series of command as below
convert.bat name1.eps name1.pdf
convert.bat name2.eps name2.pdf
.....
.....
the convert.bat first compares the modified time of name.eps and name.pdf, and if the modified time of name.eps is newer than name.pdf, then it will run epstopdf name.eps, otherwise it will do nothing.
I am really a newbie in using cmd batch. could somebody teach me how to realize the whole process I mentioned above. Thank you so much!
I figure out a way. The following batch file do the whole job.
SETLOCAL ENABLEDELAYEDEXPANSION
for %%G in (*.eps) do #if not exist "%%~nG.pdf" (epstopdf "%%G") else (
(for /f "delims=" %%i in ('dir /B /O:D "%%G" "%%~nG.pdf"') do set newest=%%~xi) & (
if !newest!==.eps epstopdf "%%G"))
Need to be able to "schedule" a command line action using windows scheduler.
This is my command:
for /r C:\Users\bob\Downloads\Done\ %f in (*) do #move "%f" C:\Users\bob\Downloads\Done\
It flattens all files into the main folder.
The "for" command does not work as in a scheduler but is working in cmd.exe
I need a batch file or other code so that I can use the scheduler.
I read these, but I'm just not that cool:
How to copy files from folder tree dropping all the folders with Robocopy?
How does "FOR" work in cmd batch file?
Take your above code and save it in a .bat file. The only change you need to make, is that percents (%) need to be doubled-up when run inside a batch file.
for /r C:\Users\bob\Downloads\Done\ %%f in (*) do (
#move "%%f" C:\Users\bob\Downloads\Done\
)
I am currently doing this to execute a single command on a particular type of files in directory.
COPY *.prn /B \\\\{$PC}\\{$PRINTER}
The PC And Printer Part is redundant no need to understand that
Instead of executing all files at once I want to be able to do one file at a time through a loop
try this:
for %%i in (*.prn) do COPY "%%~i" /B \\\\{$PC}\\{$PRINTER}
Im not entirely sure what you mean but try this, it will execute the command once for each file in the current directory and (all subdirectories, but this exact snipets not ideal for subdirectories) ending with the extension .prn:
for /r %%a in (*) do (
if %%~xa == .prn (
copy %%~na%%~xa /B \\\\{$PC}\\{$PRINTER}
)
)
Tell me if this doesn't work or you want to do this for subdirectories as well.
Yours, Mona
So I've been trying to create a batch file for a piece of software called DiscEX the software requires command line use from cmd.exe windows xp or higher the way it's initiated is like this discex (any arguments needed) location of iso file.
Now I can get the software to run using the batch file but I can't seem to figure out how to copy the target location of a file that was dragged onto it to open the batch file up
Here is what the batch file in notepad looks like.
#echo off
echo Welcome to AutoDiscEx
pause
C:\windows\system32\discex
pause
also I need to be able to start in the working directory of a portable hard drive.
All you need to do is
C:\windows\system32\discex "%1"
to get a file path argument passed into the batch
If the batch file is in the working directory already, put
cd /d %~dp0 in the batch after #echo off
If you want to determine what drive is the external usb drive, use
#echo off
setlocal
set wmi='wmic logicaldisk where "volumeserialnumber='32A78F3B'" get caption'
for /f "skip=1 delims=" %%A in (%wmi%) do (
for /f "tokens=1 delims=:" %%B in ("%%A") do (set drive=%%B)
)
echo %drive%
where volumeserialnumber is the output from vol [driveletter of USB drive:] with the - removed.
When you drag a file on a batch file, the full file path is available in the first argument (%1) of the batch file. If you need this argument to be fed to the discex application as its first argument, you can do:
#echo off
echo Welcome to AutoDiscEx
pause
C:\windows\system32\discex %1
pause