How to perform reverse of relative path in DOS? - batch-file

I want to delete some files from a folder, so I do this:
DEL "C:\Documents and Settings\name\My Documents\application\v10-105 CODE\v10-105 CODE\app\bin\Release\ExSimSeqNum-*.txt"
However, the folder "v10-105 CODE" changes very often (as version gets incremented).
In ../application/, there is only one folder and in that single folder, there are two folders.
Is there a way to generalize going down this path? I tried this:
DEL "C:\Documents and Settings\name\My Documents\application\v*\v*\app\bin\Release\ExSimSeqNum-*.txt"
but I get "The filename, directory name, or volume label syntax is incorrect."
Any suggestions?

try this:
C:
CD "C:\Documents and Settings\name\My Documents\application"
CD v*\v*
DEL app\bin\Release\ExSimSeqNum-*.txt
The reason is do not use * inside quote to match multiple characters, it won't work

Related

How to delete files matching a certain pattern in their name?

I'm creating a batch file that deletes all Rar$DIa0.??? folders in the %TEMP% directory.
Is this possible, and how to do it?
The ??? is three randomized numbers. And that's where I have trouble with - deleting all folders that have Rar$DIa0. in the name.
for /d is designed for just this type of use. Something like this should work (remove one of the % if you're testing from the command line):
for /d %%i in ("%TEMP%\Rar$DIa0.???") do rd "%TEMP%\%%i"
The /d makes it work on directory names instead of file names.
If you want to make it easier on yourself, change to the %TEMP% folder first:
pushdir
cd /d %TEMP%
for /d %%i in ("Rar$DIa0.???") do rd "%%i"
The ??? makes it only act on folders that have three letters after a .. If your folders don't have just a three letter extension, change .??? to .*. If you've got a typo, and there is no actual . in the foldername, just remove it and use Rar$DIa0??? or Rar$DIa0*
You may want to test it first by changing rd to echo to make sure you get the folders you want before actually deleting them.
For more information about for (pun intended) type for /? from a command prompt.
The command line to use in a batch file for this task is:
#for /D %%I in ("%TEMP%\Rar$DIa0.*") do #rd /Q /S "%%I"
Command FOR with option /D searches in folder defined by environment variable TEMP for subfolders with folder name starting with Rar$DIa0. not having hidden or system attribute set.
The loop variable I holds for each found subfolder matching this folder pattern the name of found folder with full path without double quotes although the path to temp folder very often contains 1 or more spaces.
For that reason just the command RD with the parameters /Q for quiet execution and /S for deleting also all subfolders in the specified folder must be called with referencing the current value of loop variable I enclosed in double quotes.
For understanding the 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.
for /?
rd /?
By the way: WinRAR deletes the temporary folders usually automatically, except a file is opened from within an archive for viewing/modifying it in another application and WinRAR is closed before the other application is exited with the opened file. In this case WinRAR can't delete the temporary folder with temporarily extracted file because the file is still opened in another application. Of course also command RD can't delete the temporary folder if this folder is still the current directory of another application or a file in this folder is still opened by another application with a read/write access lock.

dos command delete folder and files using a wild card character

In a DOS batch command window, I want to delete folders (and corresponding files within that directory) with part of the name that contains the following string (SUB
I want to start at a specfic root directory called C:\app2\proc.
I want to delete the directory and the files contained within the directory.
I want to delete folders where part of the file name is (SUB.
What I have tried so far does not work.
Here is what I have tried to far:
del /f /s /q C:\app2\proc\*(SUB*
Note: the asterik before (SUB and the asterik after (SUB is not showing up in the display of what I have tried to far.
Thus can you tell me how to solve this problem?
You can use a for loop to run through your files and send a variable (%x in this case) to rmdir with your path. Try this:
for /d %x in (*(sub*) do rmdir /s /q c:\app2\proc\%x
You porbably need to escape that ( character so that it does not get evaluated.
Checkout http://www.robvanderwoude.com/escapechars.php it seems you should replace ( with ^(

Batch script to delete files

I have a batch script as follows.
D:
del "D:\TEST\TEST1\Archive\*.TSV"
del "D:\TEST\TEST1\Archive\*.TXT"
del "D:\TEST\TEST2\Archive\*.TSV"
del "D:\TEST\TEST2\Archive\*.TXT"
del "D:\TEST\TEST 100%\Archive\*.TSV"
del "D:\TEST\TEST 100%\Archive\*.TXT"
The above code deletes all the ".txt" and ".tsv" files from all the folders except from the folder TEST 100%. For deleting the files from TEST 100% i am getting the error as The Path could not be found. I guess the % symbol in the folder name creates the issue.
Can anyone guide me to resolve the issue and to delete the files from the folder TEST 100%?
You need to escape the % with another...
del "D:\TEST\TEST 100%%\Archive*.TXT"
There's multiple ways of doing things in batch, so if escaping with a double percent %% isn't working for you, then you could try something like this:
set olddir=%CD%
cd /d "path of folder"
del "file name/ or *.txt etc..."
cd /d "%olddir%"
How this works:
set olddir=%CD% sets the variable "olddir" or any other variable name you like to the directory
your batch file was launched from.
cd /d "path of folder" changes the current directory the batch will be looking at. keep the
quotations and change path of folder to which ever path you aiming for.
del "file name/ or *.txt etc..." will delete the file in the current directory your batch is looking at, just don't add a directory path before the file name and just have the full file name or, to delete multiple files with the same extension with *.txt or whatever extension you need.
cd /d "%olddir%" takes the variable saved with your old path and goes back to the directory you started the batch with, its not important if you don't want the batch going back to its previous directory path, and like stated before the variable name can be changed to whatever you wish by changing the set olddir=%CD% line.
Lets say you saved your software onto your desktop.
if you want to remove an entire folder like an uninstaller program you could use this.
cd C:\Users\User\Detsktop\
rd /s /q SOFTWARE
this will delete the entire folder called software and all of its files and subfolders
Make Sure You Delete The Correct Folder Cause This Does Not Have A Yes / No Option
Consider that the files you need to delete have an extension txt and is located in the location D:\My Folder, then you could use the below code inside the bat file.
cd "D:\My Folder"
DEL *.txt
in batch code your path should not contain any Space so pls change your folder name from "TEST 100%" to "TEST_100%" and your new code will be
del "D:\TEST\TEST_100%\Archive*.TXT"
hope this will resolve your problem

xcopy does not create directory structure

I have a strange problem with xcopy in Windows XP Professional. I don't know if its a stupid question as I am specifying only a file as the source, so should I even expect any other behavior ? This is it:
I am using xcopy <src> <dest> /s/y.
<src>=C:\sourcefolder\a\b\c\d\something.java and
<dest>=C:\destinationfolder.
Now xcopy copies the file but does not create the directory structure \a\b\c\d\ inside C:\destinationfolder .
what I want is C:\destinationfolder\a\b\c\d\something.java and
what I get is C:\destinationfolder\something.java
I have tried to run it in destination folder C:\destinationfolder by specifying a . for target folder
Tried it without any target in above
There is a script I have which calls xcopy iteratively so I am left with C:\destinationfolder\many java files without any directory structure.
A. Yes I have done xcopy /? to see all options
B. /T also does not create any empty directory structure
C. I can not go to source folder a\b\c\d\ and run xcopy . <dest>
UPDATE
I removed my previous answer on using ROBOCOPY. I believe the following will do what you want using XCOPY.
Assuming your folder structure is like this:
SOURCE = C:\MyJavaStuff\A\B\C\D\something.java
DEST = C:\MyDestination
Run XCOPY like this:
XCOPY C:\MyJavaStuff\something*.java C:\MyDestination /S /E
Note the * in something*.java.
The problem is that you are specifying which file to copy in the source. xcopy won't create the folder structure in this case. However, if you change your call to xcopy to
xcopy *.java C:\myfolder /s/y
it will copy the .java files and the folder structure as well. You need to specify a wildcard for this call to work as you want. If you want only to copy specific files, you will have to adjust the call to xopy, e.g.:
xcopy something.jav* C:\myfolder /s/y
Edit
You say that you get the list of files to copy from another command. If you can output this list of files in a text file, you could do the following:
FOR /F "tokens=* delims=," %F in (d:\test\list.txt) DO xcopy src\%~nxF* .\dest /S /Y
What this command does is read a text file ("d:\test\list.txt" in this case), read every line, and for each file, run xcopy, adding a wildcard at the end of the file name to make sure it creates the folder structure.
I'm assuming here that:
You can get the list of files in a text file, with only the file names (and optinally the paths)
You know the source folder ("C:\sourcefolder" in your example, the folder structure "a\b\c\d" does not need to be known) and can use it in the FOR command.
You can also use the following form:
FOR /F "tokens=* delims=," %F in ('cmd') DO xcopy src\%~nxF* .\dest /S /Y
where cmd needs to be replace with the command you use to generate your list of files to copy.
Note that if you use this FOR command in a batch file, you need to replace %F with %%F (and %~nxF* with %%~nxF*).
I had a look at the xcopy switches and you can copy the directory structure with /T, although that doesn't copy empty directories you can override this with /E. So your command would look like this:
xcopy C:\sourcefolder\a\b\c\d\something.java C:\destinationfolder /T /E /S /Y
Hope this helps!
In order to get C:\destinationfolder\a\b\c\d\something.java XCOPY needs to know how much of C:\sourcefolder\a\b\c\d\something.java to duplicate.
You can use:
C:
cd \sourcefolder
XCOPY something.java* C:\destinationfolder\ /S
Just be aware that this may have the side effect of also copying C:\sourcefolder\oops\something.java to C:\destinationfolder\oops\something.java as well as any other matches for something*.java under C:\sourcefolder\.
It seems to me that xcopy is typically used for copying directory trees, not single files (though it can work). And, xcopy will recreate the directory structure under the source folder in the target folder. If xcopy is given the /i switch, the target folder is assumed to be a directory. It will be created if it does not exist, even if there are multiple parents that need to be created.
You have C:\MyJavaStuff\A\B\C\D\something.java - that is your source. You want to end up with something.java not in C:\destinationfolder, but in C:\destinationfolder\A\B\C\D - so that is your target. You don't even have C:\destinationfolder. That is OK, with /i the entire path will be created.
xcopy /i c:\MyJavaStuff\A\B\C\D\something.java C:\destinationfolder\A\B\C\D
If something.java were the only file under C:\MyJavaStuff, you could also use
xcopy /sei c:\MyJavaStuff C:\destinationfolder
That would recreate the entire tree structure, copying your file. But if there are other files (and folders) under MyJavaStuff they would also be copied.
I have written a very similar batch file using xcopy. Perhaps what I did will help you.
This is the command I used:
xcopy "c:\Data Files\Dave's Data\*.*" "m:\Dave's Data" /R/D /E/H
In this case, Dave's Data on the source contains an entire directory tree containing at least 50,000 files & exceeding 75GB data. It runs perfectly on Windows XP
I found /T was unnecessary as the directory tree is copied. I also found /S was unnecessary as /E copied directories & sub-directories including empty ones. I included /R to copy & overwrite read only files on the destination. /H copied hidden directories. /D copied only newer files. I use this as a daily backup tool for my data.
The only problem I have is while this command will work on Windows 7 the first time, it will not work on subsequent runs when the destination directory tree exists. I suspect this is due to a privilege issue as the xcopy command will work on subsequent runs on Windows 7 within a cmd.exe window.

Batch file loop to grab all folders in a directory except for a certain folder

Just wondering the best way to do this in a single batch file. I have a folder called C:\Program\Foo and I want to grab all the folders except for the testing folder inside of foo, and I want to xcopy into D:\ so in D:\ foo will be there but no test folder.
Is there a way I can loop through each folder and check for a certain name not to include?
using /Exclude would mean I would need an extra text file with "Testing" in it
I don't see why you could not create a temporary exclusion file (using a temporary folder, that is):
#ECHO OFF
FOR %%F IN ("%TEMP%\exclude.txt") DO SET tmpf=%%~sF
ECHO Testing>%tmpf%\exclude.txt
XCOPY source destination /EXCLUDE:%tmpf%\exclude.txt other options
Note: XCOPY does not recognise double quotes as path delimiters in the /EXCLUDE option and offers no alternative for specifying paths with spaces, which can be a problem on Windows XP systems. This limitation can be worked around by replacing the original path with its counterpart consisting only of short names. That is what the FOR loop in the above script does for the %TEMP% folder.
Can you use ROBOCOPY?
ROBOCOPY C:\Program\Foo D:\ * /E /XD Test
/E copies subfolders and files
/XD excludes directories
Use the EXCLUDE option and put your exclusions in that file. That will let you exclude entire directories.
http://www.pcreview.co.uk/forums/using-xcopy-backup-can-exclude-some-directories-t489674.html
xcopy "c:\document and settings" "i:\documents and settings\" /s /d /EXCLUDE:c:\a.txt
a.txt contains
\temp\
\temporary internet files\
You may need to use shorter DOS file names.

Resources