I'm trying to copy multiple files from a UNC path if a certain file does not exist in the destination directory.
SET DEST=\\ServerName\TestReleases\My Database\FolderName
if not exist "%DEST%\sqlpackage.exe" (xcopy "%DEST%\SqlPackageBinaries\*.*" "%DEST%" /S /V /I /R /Y)
%DEST%\SqlPackageBinaries\ exists and does contain files I needed.
But I got
File not found - *.*
0 File(s) copied
I also tried
SET DEST=\\ServerName\TestReleases\My Database\FolderName
if not exist "%DEST%\sqlpackage.exe" (xcopy "%DEST%\SqlPackageBinaries\" "%DEST%" /S /V /I /R /Y)
SET DEST=\\ServerName\TestReleases\My Database\FolderName
if not exist "%DEST%\sqlpackage.exe" (xcopy "%DEST%\SqlPackageBinaries" "%DEST%" /S /V /I /R /Y)
SET DEST="\\ServerName\TestReleases\My Database\FolderName"
if not exist "%DEST%\sqlpackage.exe" (xcopy "%DEST%\SqlPackageBinaries\*.*" "%DEST%" /S /V /I /R /Y)
But none of above works. I think it has something to do with the UNC but don't know what would be the correct syntax.
So it's kind of stupid that my major problem actually is the source should not be %DEST%\SqlPackageBinaries\ but \\ServerName\TestReleases\My Database\SqlPackageBinaries\.
And to be honest, I copy the command from another section of the code and modified the content that I didn't really look into the meaning of each parameter like /S as you mention. I simply assume that would be something like do not prompt or overwrite if the file exists.
Always enclose your set command variable and value in double quotes, as in my example. Then, although xcopy is still available on windows devices, it has been deprecated. Use robocopy instead:
#echo off
set "dest=\\ServerName\TestReleases\My Database\FolderName"
if exist "%dest%\sqlpackage.exe" goto :EOF
robocopy "%dest%\SqlPackageBinaries" "%dest%"
I have a list of folders and files in a text file, (%File_list%). My batch reads from the text file for the objects to copy. I need it to copy files and folders from one within a folder (%src_folder%), to another folder, (%dst_folder%).
My current attempt copies the files, but not the folders.
Batch script:
#echo off
set src_folder=C:\Users\P57949\Desktop
set dst_folder=C:\Users\P57949\Desktop\NewTestFolder
set file_list=C:\Users\P57949\Desktop\Filelist.txt
if not exist "%dst_folder%" mkdir "%dst_folder%"
for /f "delims=" %%f in (%File_list%) do (
xcopy /y /s "%src_folder%\%%f" "%dst_folder%\"
)
Text file:
Fixed Retail.xlsx
SecureFolder
Can someone please advise what I am doing wrong? How can I copy folders as well as file?
When the source is a directory, the xcopy command copies its contents rather than the whole directory. To copy the whole directory, you need to alter the destination accordingly:
xcopy /I /Y /S "%src_folder%\%%f" "%dst_folder%\%%f"
Note the /I option, that tells xcopy that the destination is not a file but a directory.
However, when the source is a single file, xcopy prompts you whether the destination is a file or directory when the destination does not yet exist, even when /I is provided (refer to this related post). Therefore, we have to determine what the source (%%f) is in advance and react adequately:
#echo off
rem // The quoted `set` syntax protects special characters:
set "src_folder=C:\Users\P57949\Desktop"
set "dst_folder=C:\Users\P57949\Desktop\NewTestFolder"
set "file_list=C:\Users\P57949\Desktop\Filelist.txt"
rem // Append `\*` to check a directory but not a file for existence:
if not exist "%dst_folder%\*" mkdir "%dst_folder%"
rem // Use `usebackq` option and quotation to permit spaces in file path:
for /F "usebackq delims=" %%f in ("%File_list%") do (
rem // Check whether source is a directory:
if exist "%src_folder%\%%f\*" (
rem // Source is a directory, so copy it entirely:
xcopy /I /Y /S "%src_folder%\%%f" "%dst_folder%\%%f"
) else (
rem // Source is a file, or it does not exist:
xcopy /Y /S "%src_folder%\%%f" "%dst_folder%\"
)
)
A bit simpler and more compact approach can be achieved when a destination file is pre-created, so the file/directory prompt does not appear:
#echo off
rem // The quoted `set` syntax protects special characters:
set "src_folder=C:\Users\P57949\Desktop"
set "dst_folder=C:\Users\P57949\Desktop\NewTestFolder"
set "file_list=C:\Users\P57949\Desktop\Filelist.txt"
rem // Append `\*` to check a directory but not a file for existence:
if not exist "%dst_folder%\*" mkdir "%dst_folder%"
rem // Use `usebackq` option and quotation to permit spaces in file path:
for /F "usebackq delims=" %%f in ("%File_list%") do (
rem // Pre-create destination file when source is a file:
if not exist "%src_folder%\%%f\*" > "%dst_folder%\%%f" rem/
rem // No prompt appears even when source is a file:
xcopy /I /Y /S "%src_folder%\%%f" "%dst_folder%\%%f"
)
The easiest approach is to use the /I option to cover the case when source is a directory and to auto-fill the file/directory prompt for the case source is a file, but this is locale-dependent (for instance, File and Directory for English systems, but Datei and Verzeichnis for German ones), so be careful:
#echo off
rem // The quoted `set` syntax protects special characters:
set "src_folder=C:\Users\P57949\Desktop"
set "dst_folder=C:\Users\P57949\Desktop\NewTestFolder"
set "file_list=C:\Users\P57949\Desktop\Filelist.txt"
rem // Append `\*` to check a directory but not a file for existence:
if not exist "%dst_folder%\*" mkdir "%dst_folder%"
rem // Use `usebackq` option and quotation to permit spaces in file path:
for /F "usebackq delims=" %%f in ("%File_list%") do (
rem // The `/I` option copes for directories, `echo F` for files:
rem echo F| xcopy /I /Y /S "%src_folder%\%%f" "%dst_folder%\%%f"
)
N. B.:
For all of the above, I assumed that the list file Filelist.txt contains pure file/directory names.
Following
Move all files except some (file pattern) from a DOS command
I want to copy all files from my release dir to the deployment dir with that batch file:
echo off
set "source=.\EasyRun\bin\Release\"
set "destination=C:\temp\EasyRun\"
IF exist %destination% rd %destination% /s /Q
IF exist %source% ( echo "OK release") ELSE ( echo "NO Realease DIR "%source% && pause && exit )
dir /b /a-d %source%|findstr /b "vshost" > excludeList.txt
xcopy /s %source%"*.exe" %destination% /exclude:excludeList.txt
xcopy /s %source%"*.dll" %destination%
echo "OK!!"
Pause
So here are two problems:
the excludeList is not filled and the file is empty
even when manually I put .\EasyRun\bin\Release*.vshost.exe it the xcopy doesn't exclude it from copying.
thanks
It might be caused by the empty excludeList.txt. -> Try to enter a dummy entry
I had some issues with xcopy and network shares.
(I got errors due to latency, access restrictions, ...)
I recommend robocopy with the /xf option. e.g. /xf vshost
Also the /MT[:N] multi-threaded copies should speed-up the copy process.
I am trying to run a simple xcopy batch script but I am facing some difficoulties.
Here is the script
SET SRC=C:\FOLDER1\FOLDER2
SET DEST=V:\FOLDER1
FOR /D %%d in (%SRC%\*) do xcopy /S /I /y /exclude:%SRC%\exclude.txt %%d V:\FOLDER1\%%~nxd
Basically this script should copy some of the subfolders of C:\FOLDER1\FOLDER2 (excluded.txt contains a list of directories to exclude) and its content to the destination. However when I run the scripts, altough no errors are thrown, NO FILES OR FOLDERS get copied. What am I doing wrong?
Interestingly if I run the following script INSIDE FOLDER2, everything procedes as expected.
FOR /D %%A in (*) DO xcopy /S /I /y /exclude:exclude.txt %%A V:\FOLDER1\%%A
xcopy "C:\FOLDER1\FOLDER2" "V:\FOLDER1" /e /i /y
You mention nothing about excluding any folders.
I want to have a batch file that will delete all the folders and files in my cache folder for my wireless toolkit.
Currently I have the following:
cd "C:\Users\tbrollo\j2mewtk\2.5.2\appdb\RMS"
del *.db
This will delete all .db files in my RMS directory, however I want to delete every single thing from this directory. How can I do this?
Use:
Create a batch file
Copy the below text into the batch file
set folder="C:\test"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
It will delete all files and folders.
del *.* instead of del *.db. That will remove everything.
IF EXIST "C:\Users\tbrollo\j2mewtk\2.5.2\appdb\RMS" (
rmdir "C:\Users\tbrollo\j2mewtk\2.5.2\appdb\RMS" /s /q
)
This will delete everything from the folder (and the folder itself).
I just put this together from what morty346 posted:
set folder="C:\test"
IF EXIST "%folder%" (
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
)
It adds a quick check that the folder defined in the variable exists first, changes directory to the folder, and deletes the contents.
del *.* will only delete files, but not subdirectories. To nuke the contents of a directory, you can use this script:
#echo off
setlocal enableextensions
if {%1}=={} goto :HELP
if {%1}=={/?} goto :HELP
goto :START
:HELP
echo Usage: %~n0 directory-name
echo.
echo Empties the contents of the specified directory,
echo WITHOUT CONFIRMATION. USE EXTREME CAUTION!
goto :DONE
:START
pushd %1 || goto :DONE
rd /q /s . 2> NUL
popd
:DONE
endlocal
The pushd changes into the directory of which you want to delete the children. Then when rd asks to delete the current directory and all sub directories, the deletion of the sub directories succeed, but the deletion of the current directory fails - because we are in it. This produces an error which 2> NUL swallows. (2 being the error stream).
You can do this using del and the /S flag (to tell it to recurse all files from all subdirectories):
del /S C:\Path\to\directory\*
The RD command can also be used. Recursively delete quietly without a prompt:
#RD /S /Q %VAR_PATH%
Rmdir (rd)
set "DIR_TO_DELETE=your_path_to_the_folder"
IF EXIST %DIR_TO_DELETE% (
FOR /D %%p IN ("%DIR_TO_DELETE%\*.*") DO rmdir "%%p" /S /Q
del %DIR_TO_DELETE%\*.* /F /Q
)
Use
set dir="Your Folder Path Here"
rmdir /s %dir%
mkdir %dir%
This version deletes without asking:
set dir="Your Folder Here"
rmdir /s /q %dir%
mkdir %dir%
Example:
set dir="C:\foo1\foo\foo\foo3"
rmdir /s /q %dir%
mkdir %dir%
This will clear C:\foo1\foo\foo\foo3.
(I would like to mention Abdullah Sabouin's answer. There was a mix up about me copying him. I did not notice his post. I would like to thank you melpomene for pointing out errors!)
Try the following; it works for me.
I have an application which dumps data in my "C:\tmp" folder, and the following works the best for me. It doesn't even ask Yes or No to delete the data. I have made a schedule for it to run after every 5 minutes
cd "C:\tmp"
del *.* /Q
Better yet, let's say I want to remove everything under the C:\windows\temp folder.
#echo off
rd C:\windows\temp /s /q
You could use robocopy to mirror an empty folder to the folder you are clearing.
robocopy "C:\temp\empty" "C:\temp\target" /E /MIR
It also works if you can't remove or recreate the actual folder.
It does require an existing empty directory.
I would like to suggest using simple tool like cleardir. So, in batch file you can write:
cleardir path/to/dir
And you'll get empty directory dir. A bit slow, but still resolves the "problem".
I'm an author of the tool =)
The easiest way is:
Create *.txt file
Write:
rmdir /q /s . dir
Save file as *.bat in folder which you want to clear (you can call the file NUKE.bat)
Turn it on
WARNING!
THIS DELETES EVERYTHING IN THE FOLDER WHERE IT IS WITHOUT ASKING FOR CONFIRMATION!!!
SO CHOOSE WISELY PLACE FOR SAVING IT.
Easy simple answer :
C:
del /S /Q C:\folderName\otherFolderName\*
C: Important in case you have to switch from D: to C: or C: to D: (or anything else)
/S Recursive, all subfolders are deleted along
/Q If you don't activate quiet mode, prompt will ask you to type y for every subfolders... you don't want that
Be carful, it's drastic.
You cannot delete everything with either rmdir or del alone:
rmdir /s /q does not accept wildcard params. So rmdir /s /q * will error.
del /s /f /q will delete all files, but empty subdirectories will remain.
My preferred solution (as I have used in many other batch files) is:
rmdir /s /q . 2>NUL
Just a modified version of GregM's answer:
set folder="C:\test"
cd /D %folder%
if NOT %errorlevel% == 0 (exit /b 1)
echo Entire content of %cd% will be deleted. Press Ctrl-C to abort
pause
REM First the directories /ad option of dir
for /F "delims=" %%i in ('dir /b /ad') do (echo rmdir "%%i" /s/q)
REM Now the files /a-d option of dir
for /F "delims=" %%i in ('dir /b /a-d') do (echo del "%%i" /q)
REM To deactivate simulation mode remove the word 'echo' before 'rmdir' and 'del'.
#echo off
#color 0A
echo Deleting logs
rmdir /S/Q c:\log\
ping 1.1.1.1 -n 5 -w 1000 > nul
echo Adding log folder back
md c:\log\
You was on the right track. Just add code to add the folder which is deleted back again.