I have a folder D:\test1 and I have another folder inside that whose name is not known.
I tried using rmdir /s "D:\test\*", but it is showing an error.
I just want to know is there any way of doing this operation?
In shell script if we give rmdir "D:\test1\*" it will remove all directories inside test1.
I am looking for the command in batch syntax.
I suggest
#echo off
for /D %%F in ("D:\test1\*") do rmdir /S /Q "%%F"
It deletes all subfolders in D:\test1 recursively even if subfolders contain one or more spaces in folder name.
Related
First of all, similar questions have been answered in past but not exactly the one I have. In some other solutions, hiding folders/files and changing attributes were suggested and I do not want this, unless there is no simpler way available. Also, I have tried the solution suggested here (and couple of others): MS-DOS command to delete all files except one.
But did not work for my need due to two reasons:
This also deleted the file, I did not want to.
This did not delete the sub-folders but only the files.
So, I have folder c:/users/data and in there, I have 5 folders and 6 files I want to delete everything except one which is web.config and to do that I run the batch file like this:
#echo off
for %%j in (C:\Users\data\*) do if not %%j == Web.config del %%j
pause
But when I run the batch file, this deletes all the files including web.config and also, does not delete any of the sub-folders and if I use the /d switch then it only deletes folders not files. How can I delete both files and folders?
Here is the way I would do it:
pushd "C:\Users\data" || exit /B 1
for /D %%D in ("*") do (
rd /S /Q "%%~D"
)
for %%F in ("*") do (
if /I not "%%~nxF"=="web.config" del "%%~F"
)
popd
The basic code is taken from my answer to the question Deleting Folder Contents but not the folder but with the del command replaced by a second for loop that walks through all files and deletes only those not named web.config.
Note that this approach does not touch the original parent directory, neither does it touch the original file web.config. The great advantage of this fact is that no attributes are going to be lost (for instance, creation date and owner).
Explanation
change to root directory by pushd; in case of failure, skip the rest of the script;
iterate through all the immediate sub-folders of the root by a for /D loop and delete them recursively by rd /S with all their contents;
iterate through the files located in the root by a standard for loop; use the ~nx modifier of the loop variable to expand base name (n) and extension (x) of each file, compare it with the given file name (if) in a case-insensitive manner (/I) and delete the file by del only in case it does not match (not);
restore former working directory by popd finally;
The main problems in your code are:
that you compare more that the pure file name with the prefedined name, so there is not going to be found any match.
you needed to use case-insensitive comparison, because Windows does not care about the case of file names for searching.
you did not put quotation marks around the comparison expressions, so you are likely going to receive syntax error messages, depending on what special characters occur in the file names.
that you are using the del command only which just deletes files; to remove directories, you needed to use rd also.
I'm not sure where the web.config file is stored or if there is more than one, so ...
Only one web.config file
Just lock the file (redirect the file as input) and remove anything else
#echo off
setlocal enableextensions disabledelayedexpansion
pushd "c:\users\data" && >nul 2>nul (
<"web.config" rmdir . /s /q
popd
)
The code will
(pushd) Change to the target folder (we need to be sure this will remove information only from the intended place) setting it as the current active directory and locking it (we can not remove the current active directory). If the command can change to the folder then
(rmdir) Redirect the web.config as input to the rmdir command. This will lock the file so it can not be deleted until the command ends. The rmdir . /s /q remove anything not locked inside (and below) the current active directory
(popd) Cancel the pushd command restoring the previous active directory
Several web.config files in multiple folders
Following the approach (copy, clean, restore) pointed by #Dominique
#echo off
setlocal enableextensions disabledelayedexpansion
pushd "c:\users\data" && >nul 2>nul (
for %%t in ("%temp%\%~n0_%random%%random%%random%.tmp") do (
robocopy . "%%~ft" web.config /s
robocopy "%%~ft" . /mir
rmdir "%%~ft" /s /q
)
popd
)
The code will
(pushd) Change to the target folder (we need to be sure this will remove information only from the intended place). If the command can change to the folder then
(for) Prepare a reference (a random name) to a temporary folder to use
(robocopy) Copy only the web.config files (and their folder hierarchy) from the source folder to the temporary folder
(robocopy) Mirror the temporary folder to the source folder. This will remove any file/folder not included in the temporary copy
(rmdir) Remove the temporary folder
(popd) Cancel the pushd command restoring the previous active directory
Once the web.config files are saved, as the files in the source will match those in the temporay folder, they will not be copied back with the second robocopy call, but any file/folder in source that is not present in the temporary folder will be removed to mirror the structure in the temporary folder.
I also managed to do it, very similar to #aschipfl
For /D %%k in (C:\Users\data) do (For /d %%m in ("%%k\*") do rmdir /s /q "%%m"
For %%m in ("%%k\*") do if not %%m == %%k\Web.config del /q "%%m")
I need to be able to create a bat script to do 3 things:
Search for multiple specific filenames in a directory.
Find the most recently generated version based on each filename specified.
Copy that most file to a new dir.
I am very new to coding in general, so any assistance would be much appreciated.
So far all I have been able to do is figure out how to copy files from one location to another using the below:
xcopy /s c:\source\differentfilename1.csv d:\target\
xcopy /s c:\source\differentfilename2.txt d:\target
xcopy /s c:\source\differentfilename3.html d:\target
So far I have tried the following and its not copying the files over:
ECHO
CD D:\Data\
MKDIR D:\Data\CopyFilesHere
for /R %file in (Filename1.*) DO XCOPY "%file" D:\Data\CopyFilesHere
for /R %file in (Filename2.*) DO XCOPY "%file" D:\Data\CopyFilesHere
for /R %file in (Filename3.*) DO XCOPY "%file" D:\Data\CopyFilesHere
I have since noted there are subfolders I need to search through also.
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
I am trying to create a batch script that copies the contents of one folder into another. Ihave tried this:
mkdir "c:\Jamie"
cd c:\jamie_DateTimeStamp*
xcopy * "c:\Jamie"
for /D %%f in (c\jamie_*) do rmdir %%f /s /Q
I can get the delete to work but I cannot get the xcopy to work.
things to know
The Jamie_datetimestap folder can be any date and time so I don't have a constant and I need to use a wildcard.
The goal of the script to copy the contents of a folder that has a datetime stamp into a folder that does not have one, then delete the folder with the datetime stamp I cannot simply rename the folder.
If you want to copy subfolders with xcopy you need option /s for subdirectories.
You can not use wildcard in cd command though, but you can do a dir /b Jamie* and use this inside the for loop as you do for delete to use as target for cd.
I want to loop through all subfolders in c:\my_folder, check if specified file exist then create (if not exist) folder in c:\copy (for example c:\copy\project1) and copy mentioned file.
Is it easy to loop through folders in batch file (for /d %variable in ("path") DO command),
but variable contains full path. For creating new folder I need only basename for subfolder.
How I can get it? Should I write another for loop when I remove other characters?
Is there any easier way?
If I understand you correctly you want just the folder name. In that case this is easy:
for /d %%d in (C:\my_folder) do echo %%~nxd
What Joey said. On the other hand, you could try a completely different approach:
XCOPY "C:\my_folder\my_file" "C:\copy" /S /I
This will copy the specified file from all subfolders of C:\my_folder to C:\copy with preserving the structure. Subfolders that do not have my_file will not be created in C:\copy. The /S switch tells XCOPY to search for my_file in subfolders. The /I parameter is there to make sure C:\copy should be a folder, before proceeding.