Pass list of file paths to a batch file to be copied from Location A to Location B - excel

Beginner to batch files and running commands in cmd or PowerShell. Need help in writing a script and/or producing a batch file that will allow me to do the following:
1) Pull list (List1) of file paths (including the file name and where the file is saved currently) from ColumnA of Excel123.xslx and a list (List2) of new destination file paths from ColumnB of Excel123.xlsx (if needed, the file with the old path and destination path can be in CSV or any txt format)
2) Pass through the file paths from List1 and copy the file from the location in List1 to the new destination location in List2
In fewer words, I need to copy a file from the current location in List1 to the new destination location in List2. And, if the new destination location includes folders and sub-folders that do not exist yet, they need to be created in the process.
Any advice/insight? Thank you!

I believe you are looking to essentially do as follows:
#(SETLOCAL
ECHO OFF
SET "_CSV_File=C:\Some\Path\FileList.csv"
)
FOR /F "Tokens=1-2 Delims=," %%A IN ('
Type %_CSV_File% ') DO (
ECHO: Copying FROM: "%%~A" TO: "%%~B"
COPY /B /V /Y /Z "%%~A" "%%~B"
)
ENDLOCAL

Related

Batch - Find string in text file and delete full line

I use the script from this Thread accepted answer from Mofi. The script copy folders and store them in text file to exclude once copied folders on next run.
Sometimes I have folders called [incomplete]-different.names and I do not want to copy this folders. I want that all folders with the string [incomplete]- and the name behind are skipped or are not even written in the text file %CurrentList% for further processing.
These are my previous attempts but so far I could not get up and running with the script from the top.
Help would be nice, and thanks in advance.
Try 1:
for /f "delims=" [incomplete]-%%D in ("%CurrentList%") do (
set str=%%D
set str=!str: =!
set str=!str: %%D =!
echo !str!
Try 2:
findstr /v /b /c:"[incomplete]-"%%D" "%CurrentList%" del "%%D"
You were really close with your second attempt.
If all you want is to delete lines in %CurrentList% that contain the string [incomplete]-, you can just direct the output of a findstr /v to a temp file and then overwrite CurrentList with that file.
findstr /v /c:"[incomplete]-" "%CurrentList%" >tmpList.txt
move /y tmpList.txt "%CurrentList%" >nul

what is Batch file (.bat) equivalent for if [-f $path ] [duplicate]

I have to create a .BAT file that does this:
If C:\myprogram\sync\data.handler exists, exit;
If C:\myprogram\html\data.sql does not exist, exit;
In C:\myprogram\sync\ delete all files and folders except (test, test3 and test2)
Copy C:\myprogram\html\data.sql to C:\myprogram\sync\
Call other batch file with option sync.bat myprogram.ini.
If it was in the Bash environment it was easy for me, but I do not know how to test if a file or folder exists and if it is a file or folder.
You can use IF EXIST to check for a file:
IF EXIST "filename" (
REM Do one thing
) ELSE (
REM Do another thing
)
If you do not need an "else", you can do something like this:
set __myVariable=
IF EXIST "C:\folder with space\myfile.txt" set __myVariable=C:\folder with space\myfile.txt
IF EXIST "C:\some other folder with space\myfile.txt" set __myVariable=C:\some other folder with space\myfile.txt
set __myVariable=
Here's a working example of searching for a file or a folder:
REM setup
echo "some text" > filename
mkdir "foldername"
REM finds file
IF EXIST "filename" (
ECHO file filename exists
) ELSE (
ECHO file filename does not exist
)
REM does not find file
IF EXIST "filename2.txt" (
ECHO file filename2.txt exists
) ELSE (
ECHO file filename2.txt does not exist
)
REM folders must have a trailing backslash
REM finds folder
IF EXIST "foldername\" (
ECHO folder foldername exists
) ELSE (
ECHO folder foldername does not exist
)
REM does not find folder
IF EXIST "filename\" (
ECHO folder filename exists
) ELSE (
ECHO folder filename does not exist
)
Here is a good example on how to do a command if a file does or does not exist:
if exist C:\myprogram\sync\data.handler echo Now Exiting && Exit
if not exist C:\myprogram\html\data.sql Exit
We will take those three files and put it in a temporary place. After deleting the folder, it will restore those three files.
xcopy "test" "C:\temp"
xcopy "test2" "C:\temp"
del C:\myprogram\sync\
xcopy "C:\temp" "test"
xcopy "C:\temp" "test2"
del "c:\temp"
Use the XCOPY command:
xcopy "C:\myprogram\html\data.sql" /c /d /h /e /i /y "C:\myprogram\sync\"
I will explain what the /c /d /h /e /i /y means:
/C Continues copying even if errors occur.
/D:m-d-y Copies files changed on or after the specified date.
If no date is given, copies only those files whose
source time is newer than the destination time.
/H Copies hidden and system files also.
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.
/T Creates directory structure, but does not copy files. Does not
include empty directories or subdirectories. /T /E includes
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
`To see all the commands type`xcopy /? in cmd
Call other batch file with option sync.bat myprogram.ini.
I am not sure what you mean by this, but if you just want to open both of these files you just put the path of the file like
Path/sync.bat
Path/myprogram.ini
If it was in the Bash environment it was easy for me, but I do not
know how to test if a file or folder exists and if it is a file or
folder.
You are using a batch file. You mentioned earlier you have to create a .bat file to use this:
I have to create a .BAT file that does this:
Type IF /? to get help about if, it clearly explains how to use IF EXIST.
To delete a complete tree except some folders, see the answer of this question: Windows batch script to delete everything in a folder except one
Finally copying just means calling COPY and calling another bat file can be done like this:
MYOTHERBATFILE.BAT sync.bat myprogram.ini

move files name in excel to a specific folder

I am trying to write a batch file that reads from excel file. then move them to a specique folder.
lets say in the excel , there are:
111.txt
222.txt
333.txt
512.txt
I want a batch file that move files into a specific directory that have the above names.
how can I achieve that ?
(I am using xp , and I the directory is D:)
cd /d "d:\files"
for /f %%i in ("C:\where the file resides\file.csv") do (
echo move "%%i" "D:\New\"
)
remove the echo if output seems ok

Batch file Copy Folder specified in a .txt file

I'm looking for batch file code that will copy folders specified in a text file.
So in the text file
C:\User\
C:\Random\Random\Random
and so on.
The destination will be set at N:\Backups\PC
Thanks
set dest=N:\Backups\PC
for /f %%i in (C:\dirs.txt) do copy "%%i" %dest%

How to set a string from a txt to a variable with cmd

I have a txt file to a specific path that contains one or more line just like
the following:
Directory of c:\folderA\folderB
Directory of c:\folderC\folderD
these are the paths of a program example.exe. I want automatically to find the path that example.exe exists in my computer and to run it from a .cmd script. So far i have succeed to log the paths of existence to a txt. How can I set a variable with only the path of
the first line?
The final result should be var_path=c:\folderA\folderB
A quick and dirty solution is:
#echo off
Call :getFirstDirectory
::do your stuff with %var_path%. For example:
#echo %var_path%
exit /b
:getFirstDirectory
for /f "tokens=3" %%a in (dirs.txt) do (
set var_path=%%a
exit /b
)
Which assumes that dirs.txt is the file containing:
Directory of c:\folderA\folderB
Directory of c:\folderC\folderD

Resources