Rmdir command not working after copying war file using windows batch - batch-file

I write windows batch file for copying war file. Then I remove the directory that contain war file but the directory does not disappear. See my command
copy D:\target\*.war D:\new_target
IF exist D:\target (
rmdir D:\target /s /q
)
But my folder "target" can't delete.Is there any comments for delete folder batch command.
I use rd instead of rmdir but the same result.I use Windows 7, 64bit.

MC ND explained most likely the reason for denied deletion. Another one would be no permission to delete the folder with the used user account according to NTFS permissions.
#echo off
if exist "D:\target\*.war" (
cd /D D:\
copy "D:\target\*.war" "D:\new_target"
rd "D:\target" /s /q
)
Double quotes are used around all folder/file specifications in case of your real folders contain 1 or more spaces.
The batch file sets the current working directory to root of drive D: to avoid that directory D:\target is the current working directory of command line interpreter running this batch file.
But the executed batch file should nevertheless not be stored in D:\target.

Related

How to differentiate between system and user files in the batch language?

I am looking to copy all text files on the C drive, but the program needs to avoid text files that are part of the system, and only copy the text files that have been created by the user.
Is there a built in way to do this, or do I need to get creative? If so, how would you go about it? I would like to keep it within batch and not involve powershell or anything like that.
I thought about using creation dates to determine whether or not a file is a system file, but that didn't work all the way.
You can generate a list of all systemfiles using the dir command. So
dir /b /as > %USERPROFILE%\excluded_Files.txt
Would generate a list of the systemfiles from that directory.
Using xcopy as described here you can now copy all files from one directory to another excluding the systemfiles.
The xcopy command should be something like this:
xcopy C:\firstDirectory\*.txt C:\secondDirectory /EXCLUDE:%USERPROFILE%\excluded_Files.txt
Edit
So your code that is there would copy all rtf and txt files from the whole C drive to Drive D folder E.
A batch-file using my method would look like this:
#echo off
cd /d C:\
dir /b /as /s > %USERPROFILE%\excluded_Files.txt
xcopy C:\*.rtf D:\E /EXCLUDE:%USERPROFILE%\excluded_Files.txt /s
xcopy C:\*.txt D:\E /EXCLUDE:%USERPROFILE%\excluded_Files.txt /s
Explanation:
#echo off supresses redundant output of the commands that would be displayed before execution.
cd /d C:\ changes the current execution directory to the root of the C drive. /d is added for potential drive change.
dir /b /as /s > %USERPROFILE%\excluded_Files.txt redirects the output of the dir command into a file in your userprofile (Usually C:\Users\yourUserName). The switch /b will only show the filenames and not size, creation date etc. /as will only list system files and /s makes the dir command work recursively through all directories.
xcopy C:\*.rtf D:\E /EXCLUDE:%USERPROFILE%\excluded_Files.txt /s will copy all rtf files from C:\ to D:\E and exclude all files from the list previously created (that will contain all system txt and rtf files (as well as others)). /s makes xcopy work recursively as for dir.
Next line is a copy of the same with txt files.

Why is a folder not deleted with RMDIR in batch file as last command?

I have the following code in a .bat file:
#echo off
xcopy /Y /S %CD%\Code\Release C:\Users\%USERNAME%\Desktop\ShareIt /I
cls
cd C:\Users\%USERNAME%\Desktop\ShareIt\
call "Trabalho AEDA.exe"
xcopy /Y /S C:\Users\%USERNAME%\Desktop\ShareIt\FICHEIROS\ %CD%\Code\Release\FICHEIROS\
RMDIR /S /Q C:\Users\%USERNAME%\Desktop\ShareIt
That copies a folder to a location, runs the .exe from it and then it overwrites the original files in my folder and has do delete the ones initially copied.
The folder I copy to the user desktop has other folder inside, and the .exe.
At the final line of the .bat, it deletes everything in the folder, but the folder is kept in the Desktop folder. I want to delete it, too. I tried several instructions, but without success.
EDIT: That was the issue, thanks guys.
ShareIt folder isn't deleted probably because you are in the folder.
So, adding cd .. before RMDIR /S /Q C:\Users\%USERNAME%\Desktop\ShareIt solves it.
The main problem with deleting ShareIt folder is already answered by the other answers.
It is not possible on Windows to delete a folder which is the current working directory of any running process or in which a file is opened by an application with a file lock preventing the deletion of the opened file.
In this case the ShareIt folder is the current working directory of the command process executing the batch file because the batch file explicitly sets this directory as working directory. The solution is making any other directory the working directory for the command process.
But this is not the only potential problem of the few command lines of this batch file. There are some more.
%CD% could expand to a path which contains perhaps one or more spaces or other characters like &()[]{}^=;!'+,`~ which require enclosing complete folder path in double quotes. This list of characters is output on running in a command prompt window cmd /? in last paragraph on last output help page.
Also %CD% expands to a folder path usually not ending with a backslash, except the current directory is the root directory of a drive. So %CD%\Code\Release could for example expand to C:\\Code\Release with two backslashes in path. However, this is no real problem as Windows kernel corrects the path automatically on whatever file system access function is used internally by xcopy for copying the files and folders.
Parameter /I lets xcopy interpret the target string as folder path even if folder path is not ending with a backslash, but only if multiple files are copied like when source path is a folder path like it is here obviously the case. Otherwise on copying just a single file /I is ignored by xcopy. Best for a folder path as target is specifying the target folder path with a backslash as then xcopy interprets target always as folder path. It is easier to use here paths relative to current directory and omit %CD% completely.
C:\Users\%USERNAME% is not good as the user's profile directory can be also on a different drive than C: and can be in a different directory than Users, for example on Windows XP. The path C:\Users\%USERNAME% is the default for the user's profile directory since Windows Vista. But every user has the freedom to change it also on Windows Vista and later Windows versions. There is the environment variable USERPROFILE defined by Windows which contains full path to active user's profile directory. See Wikipedia article about Windows Environment Variables for more details about predefined Windows environment variables.
And of course the user's profile directory path could contain one or more spaces or other characters which again require double quotes around complete folder path or file name, for example if the user account name contains a space character. So again double quotes should be used wherever %USERPROFILE% or %USERNAME% is used in a folder or file name with path.
Command CD without parameter /D changes the current directory only on current drive if the new current directory exists at all on current drive. So with current directory on starting the batch file being on a different drive than drive C:, changing the current directory with used code would not work at all.
The command CALL should not be necessary at all in case of Trabalho AEDA.exe is really an executable as the file extension indicates. CALL can be used also for an executable, but it is designed primary for running a subroutine or calling another batch file from within a batch file.
The last two lines do not really make sense because the current directory was changed by the batch file to ShareIt folder in desktop folder of current user. Therefore %CD%\Code\Release\FICHEIROS\ expands to
C:\Users\%USERNAME%\Desktop\ShareIt\Code\Release\FICHEIROS\
as target path for xcopy and the last line deletes the entire folder
C:\Users\%USERNAME%\Desktop\ShareIt
That is obviously not the intention. xcopy in last but one line should copy the files to a subdirectory in initial current directory.
I suggest to use this batch code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\xcopy.exe Code\Release "%USERPROFILE%\Desktop\ShareIt\" /C /Q /R /S /Y >nul
pushd "%USERPROFILE%\Desktop\ShareIt"
if not errorlevel 1 "Trabalho AEDA.exe" & popd
%SystemRoot%\System32\xcopy.exe "%USERPROFILE%\Desktop\ShareIt\FICHEIROS" Code\Release\FICHEIROS\ /C /Q /R /S /Y >nul
setlocal EnableDelayedExpansion
if "!CD:%USERPROFILE%\Desktop\ShareIt=!" == "!CD!" RMDIR /S /Q "!USERPROFILE!\Desktop\ShareIt"
endlocal
endlocal
The last IF conditions needs most likely an additional explanation for what is its purpose.
!CD:%USERPROFILE%\Desktop\ShareIt=! results in searching case-insensitive in current directory path string for all occurrences of the path string to ShareIt directory in desktop directory of current user and replacing all found occurrences by an empty string. So it depends on what is the current directory on what happens here.
In case of current directory is %USERPROFILE%\Desktop\ShareIt, the string left of comparison operator == becomes "". In case of current directory is a subdirectory of %USERPROFILE%\Desktop\ShareIt, the string left of == becomes a relative path to this directory enclosed in double quotes. In all other cases the string left of == is not modified at all and expands to path of current directory enclosed in double quotes like the string right of the comparison operator.
So the IF condition checks if the current directory is NOT the ShareIt directory or a subdirectory of this directory in the desktop directory of current user. Only in this case it is safe to delete the entire ShareIt directory.
Note: The IF condition does not work correct on %USERPRFOLE% expands to a folder path string containing one or more ! because of enabled delayed variable expansion.
For understanding all other used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
cd /?
echo /?
endlocal /?
if /?
popd /?
pushd /?
set /?
setlocal /?
xcopy /?
There should be read also the Microsoft documentation about Using command redirection operators for an explanation of >nul.
Since you execute
cd C:\Users\%USERNAME%\Desktop\ShareIt\
It's the current directory when you execute the now delete the directory command.
Move to another directory, and then try the deletion

Batch script doesn't function properly

So this is what I have so far. The problem I'm having is it archives the file to 7zip and when I try to tell it to move this file it says it's not there. I'm not sure if there's an easier way to move, zip, and rename a file and move it again to another folder.
#Echo Off
xcopy "\\READYSHARE\USB_Storage\Address Book\Address Book" "C:\Users\Service Department\Desktop\Zip"
7za a –tzip "C:\Users\Service Department\Desktop\ZIpped" "C:\Users\Service Department\Desktop\Zip" /s /e
pause
It is always advisable to read the documentation of the commands to use before creating a batch file.
xcopy (Microsoft Windows XP documenation, easier to read) or xcopy (Microsoft TechNet article)
Documentation for 7-Zip standalone command line tool 7za is installed together with 7-Zip in program files folder of 7-Zip. The file to view is 7-zip.chm, a Windows help file.
The switches /s and /e belong to command xcopy, but are appended to call of 7za. /s is for copying also subdirectories, but not empty subdirectories. /e is for copying also subdirectories including empty subdirectories. It is possible to specify both, but usually just /e needs to be specified to copy a directory tree completely using xcopy.
To recursively archive all files and folders of a folder with 7za the switch -r must be used according to 7-Zip documentation.
#echo off
%SystemRoot%\System32\xcopy.exe "\\READYSHARE\USB_Storage\Address Book\Address Book" "%USERPROFILE%\Desktop\Zip" /E /I
"%ProgramFiles%\7-Zip\7za.exe" a –tzip "%USERPROFILE%\Desktop\Zipped.zip" -r "%USERPROFILE%\Desktop\Zip"
pause
I have not executed this batch file, but it should work if path to 7za.exe is correct on your computer.

batch file copies folder and pastes it the location where the batch file is run from

This is a batch based skill required program
That is used to backup data
The batch file is stored on a removable device like:
Pendrive
Flashdrive
SDCard (memory card)
When the batch file is executed it must collect a folder found at location "X"
D:/music
All data in this folder must be copied to the location of where the batch file is
If the batch file is located at any of these points
E:/
F:/
G:/
Even
Z:/
The batch operation should know where it is located and copy the music folder and paste it where the batch file is run from
My device when connected via USB changes sometimes
(E:/music)
(F:/music)
Batch file root folder
PC
E:/music (fixed location)
Code
Batch copies all content in E:/music
Batch pastes all Content to drive E:/ or F:/
Without errors
Batch file must know where it is
Can you please help me with this its very useful and resourceful because I can save content to my device and its very small
This simple batch file does the job:
#echo off
if exist "D:\music\*" (
echo Deleting %~d0\music ...
rd /S /Q "%~d0\music" 2>nul
echo Copying D:\music to %~d0\music ...
xcopy "D:\music\*" "%~d0\music" /S /E /C /I /Q /H /K
echo Folder D:\music copied to drive %~d0
pause
)
I used the information printed by running in a command prompt window the commands:
cmd /? ... outputs help for command CMD which is the interpreter of batch files, and listing also internal commands of the command line interpreter CMD.
for /? ... outputs help for command FOR with information about ~d to get drive from a string like argument 0 of the batch file referenced by %0 which is the name of the batch file with path.
rd /? ... outputs help for command RD with information how to silently delete an entire directory tree.
xcopy /? ... outputs help for command XCOPY with information how to silently copy an entire directory tree.
The command RD is used to first remove the target directory on removable media before copying the files from source directory to avoid a mixture of new and old folders and files.
The command XCOPY is used to copy the entire directory tree of D:\music to the removable media nearly completely silent. Just the total number of copied files is output which I think is good to see.

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

Resources