Dos batch copy all files except one not working - dos

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.

Related

Deleting Contents of Multiple Directories, Only if They Exist (Batch File)

I'm trying to write a simple batch file that will clean up disk space. I have to delete the entire contents (folders and files) of 4 different directories, but only if they exist. I've been testing trying to delete one, but I know nothing about writing batch files. After all the research I've done, I came up with a couple lines of code that doesn't work.
#echo off
IF EXIST "C:\Windows\TestFolder\TestSubFolder\*.*"
DEL /s /q "C:\Windows\TestFolder\TestSubFolder\*.*"
for /d %%p in ("C:\Windows\TestFolder\TestSubFolder\*.*") do rmdir "%%p" /s /q
exit
In this scenario, I need to be able to delete the contents of TestSubFolder, if TestSubFolder exists. Whether it exists or not, after that action is complete, I need the code to do the same thing to a TestSubFolder2.
Thanks
The main problem in your code is the improper usage of the if command. If there is only one command to execute if the condition is true, it can be written in the same line, but to write the command in the next line you need to use parenthesis. It should be something like
IF EXIST "C:\Windows\TestFolder\TestSubFolder\*.*" (
DEL /s /q "C:\Windows\TestFolder\TestSubFolder\*.*"
for /d %%p in ("C:\Windows\TestFolder\TestSubFolder\*.*") do rmdir "%%p" /s /q
)
But this can be simplified as
2>nul pushd "C:\Windows\TestFolder\TestSubFolder" && (
rmdir . /s /q
popd
)
That is, we try to change to the indicated folder (pushd) and if there was not any problem (conditional execution operator && means execute next command if the previous one did not fail) them remove all the contents of the folder (rmdir) and return to the previous active directory (popd). The 2>nul is just hidding any error message (ex. the folder does not exist, locked files that can not be removed, ...)
Now, if the process has to be repeated for more than one folder, we can use the for command to iterate over the list of the folders
for %%a in ( "folder1" "folder2" ) do ....
Placing the previous code into this for loop we have
#echo off
setlocal enableextensions disabledelayedexpansion
2>nul (
for %%a in (
"C:\Windows\TestFolder\TestSubFolder"
"C:\Windows\TestFolder\TestSubFolder2"
) do pushd "%%~fa" && (
rmdir . /s /q
popd
)
)
The error hidding has been moved to cover all the for execution, and now, for each of the folders (referenced by the for replaceable parameter %%a), we try to change to the folder using the full path (%%~fa) and if we can change to it, then remove all the folder contents before returning to the original active directory.
CD "C:\Windows\TestFolder\TestSubFolder"
RD /s /q "C:\Windows\TestFolder\TestSubFolder"
Works for me.
or from anywhere
RD /s /q "C:\Windows\TestFolder\TestSubFolder"
MD "C:\Windows\TestFolder\TestSubFolder"

Windows script to backup specific files from directory to another on same machine

I need a help with mine school project scipt. I thought it would be easy, but apparently found myself a bit confused with it.
The task is to:
Write a script, which gets as parameters two directories. First directory must exist. From the first directory and its subfolders the backup will be done for files such as .c,.txt,.jpg,.csv... and these files will be backed up to the second directory, which is nonexistent or is empty.
I figured out just the copying part...
#echo
if %username%==administrator goto useradmin
rem # files with C
XCOPY "%USERPROFILE%\Documents\iT universe city\Source Folder\*.c" "%USERPROFILE%\Desktop\jpg\" /D /I /S /Y
rem # files with TXT
XCOPY "%USERPROFILE%\Documents\iT universe city\Source Folder\*.txt" "%USERPROFILE%\Desktop\jpg\" /D /I /S /Y
rem # files with JPG
XCOPY "%USERPROFILE%\Documents\iT universe city\Source Folder\*.jpg" "%USERPROFILE%\Desktop\jpg\" /D /I /S /Y
rem # files with CSV
XCOPY "%USERPROFILE%\Documents\iT universe city\Source Folder\*.csv" "%USERPROFILE%\Desktop\jpg\" /D /I /S /Y
You aren't usually provided homework/tasks for which you have not previously been provided sufficient information. When you are the intention is usually that you actually put in some time and effort researching.
For that reason I will only provide this. You can put in your own time and effort to look up the commands and work out how it works:
#Echo Off
Set/P "SrcDir=Enter Source Folder: "
If Not Exist "%SrcDir%\" Exit/B
Set/P "DstDir=Enter Destination Folder: "
ROBOCOPY "%SrcDir%" "%DstDir%" *.c *.txt *.jpg *.csv /S

XCOPY still asking (F = file, D = directory) confirmation [duplicate]

This question already has answers here:
Why does the command XCOPY in batch file ask for file or folder?
(11 answers)
Closed 3 months ago.
My batch script xcopy is still asking F = file, D = directory confirmation even though I have added /F in the script, the log is showing as below.
Please help on how to avoid asking confirmation.
Script:
net use p: /delete
net use p: "\\200clan\F_Drive" /USER:adm /PERSISTENT:NO-1111
set source=%1
set target=p:/%2
echo %source% %target%
xcopy /S /I /Q /Y /F "%source%" "%target%"
Log:
C:\test\foldera>xcopy /S /I /Q /Y /F "C:/test/folder1/folder2/logs/154/compareReport_177.html" "p:/Services/WSDLCompare/177_20151116/compareReport_177.html"
Does P:\Services\WSDLCompare\177_20151116\UIReport_177.html specify a file name
or directory name on the target
(F = file, D = directory)?
The /I switch (not /F as you mentioned in your question) prevents xcopy from asking whether the destination is a file or a directory only if multiple source files are given, so if the source is a directory, or if wildcards ? or * are used. If the destination already exists, such prompt does never appear.
There are the following scenarios (depending on the provided values of %source% and %target%):
a single source file, the destination is a file:
the /I switch is useless, so you need to pipe F into the xcopy command line:
echo F|xcopy /S /Q /Y /F "%source%" "%target%"
provided that the /Y switch is given (to force overwriting), you could also create the target file in advance (empty file):
>> "%target%" rem/
xcopy /S /Q /Y /F "%source%" "%target%"
a single source file, the destination is a directory:
the /I switch is useless too; you can pipe D into the xcopy command line:
echo D|xcopy /S /Q /Y /F "%source%" "%target%"
or you can simply append a \ to the destination:
xcopy /S /Q /Y /F "%source%" "%target%\"
although this causes trouble when %target% specifies the current directory of a drive like D: for instance, because D: means the current directory of this drive whereas D:\ means the root directory of it;
or you create the destination directory in advance:
2> nul mkdir "%target%"
xcopy /S /Q /Y /F "%source%" "%target%"
the 2> nul portion suppresses the error message in case the directory already exists;
multiple source files, the destination is a file:
this is usually a senseless situation, because you tell xcopy to copy each source file to the same destination file, thus attempting to overwrite it;
multiple source files, the destination is a directory:
the /I switch makes sense here:
xcopy /S /I /Q /Y /F "%source%" "%target%"
the pipe option also works here:
echo D|xcopy /S /Q /Y /F "%source%" "%target%"
so does appending a \ to the destination (regarding the limitation as mentioned above):
xcopy /S /Q /Y /F "%source%" "%target%\"
or you create the destination directory in advance:
2> nul mkdir "%target%"
xcopy /S /Q /Y /F "%source%" "%target%"
Conclusion
The most flexible and secure solution is to pipe the desired selection (F or D) into the xcopy command line. (Note that the query is locale-dependent.)
Supplement
There are some minor issues in your code fragment I want to mention here:
you should generally use the \ as a path separator as this is the Windows standard character for that purpose (although / works too in most cases);
there is -1111 appended to your second net use command line; if this constitutes the password for the resource, it should be moved before the /USER option; otherwise just remove it;
your set command lines introduce problems with some special characters (like &, ^, (, )); to avoid such, state set "source=%~1" and set "target=p:/%~2"; the ~ removes potential surrounding "" from the arguments (which are required if they contain SPACE, ,, ;, =);
Here is the code with the all of the above things reworked:
net use P: /DELETE
rem supposing `-1111` constitutes the password for the resource:
net use P: "\\200clan\F_Drive" -1111 /USER:adm /PERSISTENT:NO
set "source=%~1"
set "target=P:\%~2"
echo "%source%" "%target%"
rem supposing the destination is a directory:
echo D|xcopy /S /I /Q /Y /F "%source%" "%target%"
rem actually you do not need any interim variables:
REM echo D|xcopy /S /I /Q /Y /F "%~1" "P:\%~2"
Put a \ behind the target to suppress the question and specify it as a folder:
xcopy src dest\
Put a * behind the target to suppress the question and specify it as a file:
xcopy src dest*
Note: The second command is a hack that relies on wildcard matching (will match any item starting with "dest") so use it at your own risk!
xcopy doesn't know the target is a directory. You clarify this by putting a backslash at the end:
xcopy /S /I /Q /Y /F "%source%" "%target%\"
When copying a single file with XCOPY, there is no option to indicate if the destination is a filename or a directory (with the filename defaulting to that of the source file).
In such cases XCOPY will prompt with a (locale specific) message like:
C:> xcopy foo.txt bar.txt
it prompts
Does foo.txt specify a file name or directory name on the target (F = file, D = directory)?
Adding a wildcard (*) to the end of the destination will suppress this prompt and default to copying as a file:
C:> xcopy foo.txt bar.txt*
1 File(s) copied
This requires the source and target file extensions to be the same length, typically 3 characters.
for more information: https://ss64.com/nt/xcopy.html
Removing the destination filename will suppress the message. This works for me!
I use:
xcopy /S /I /Q /Y /F "%source%" "%target%"
It works.
Try:
echo F>xcopy_answer.tmp
xcopy xcopy /Q/Y "%source%" "%target%"<xcopy_answer.tmp

Check if files exist in cmd

How do I check if a bunch of files exist in my script?
I want my script to perform some add the files if its not present or just ignore and proceed with the rest of the code.
This is what I have writtes:
if not exist qapi/qapi*.h (
#ECHO OFF
CD %~dp0\..\..
ECHO %CD%
SET TOP_IOE_SW= %CD%
ECHO %TOP_IOE_SW%
SET BUILD_DIR=%TOP_IOE_SW%/build/ms
icacls * /q /c /t /grant Users:F
mkdir include\qapi
xcopy /Y qapi_export\qapi*.h include\qapi
xcopy /Y qapi\common\qapi*.h include\qapi
xcopy /Y core\api\qapi*.h include\qapi
xcopy /Y qapi\build include\build\
)
else (
call :next)
:next
////the rest of the code comes here///
When I run the script, it keeps prompting me to overwrite these files.
How can I have this done without the prompt?
Thanks

Delete all files and folders in a directory

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.

Resources