DOS bat
I want to find files with a certain extension, for example
where /R c:\ *.ppx
and move them all to a specified directory, for example c:\PPS
Thanks!
Redirect the output of WHERE to a file
WHERE /R C:\ *.ppx > ppxlist.txt
And then use a FOR loop to move them
FOR /f %i in (ppxlist.txt) do move %i c:\pps\
Here's a similar question that suggests a few methods:
How can I recursively copy files of a specific pattern into a single flat folder on Windows?
Related
I have a directory with some subdirectories called *.data which contain a file named Ratio.img, a file called Ratio.hdr and a directory vector_data.
This is my setup:
dir:
-ABC.data/-Ratio.img
/-Ratio.hdr
/-vector_data
-DEF.data/-Ratio.img
/-Ratio.hdr
/-vector_data
I want a command-line solution to take the Ratio.img from each subdirectory, rename it to the name of the subdirectory (without .data) and move it to the directory itself.
I tried some stuff looking for .img images and move them, but I did not manage to include the renaming to the filename.
# Unfortunately all those .img file are named Ratio, so they ask me to overwrite, what I clearly do not want to.
FOR /R %G in ("*.img") DO Move "%G" "G:\GRD_TEst\Products"
#I tried Renaming the Ratio.img-files, since I think I could work with them even if they are named blah.data.img
FOR /D %G in ("*.data") DO Rename "G:\GRD_TEst\Products\"%G"\Ratio.img" "G:\GRD_TEst\Products\"%G"\"%G".img"
# I also tried some "nesting" of for loops, but this did not lead to the output I wanted, basically create the path to my Ratio.img-File
for /D %G in (.data) do
FOR /D %X in ("Ratio.img") DO echo %G\%X
I expect the output to be like
dir:
-ABC.img
-ABC.data/Ratio.hdr
/vector_data
-DEF.img
-DEF.data/Ratio.hdr
/vector_data
I might have found a solution (I have to reproduce my Ratio.img files since I did not make backup-copies :( )
`for /D %%G in ("*.data") do (
FOR /D %%I in (Ratio.img) DO Rename G:\GRD_TEst\Products\%%G\%%I %%G.img
FOR /D %%H in (Ratio.hdr) DO Rename G:\GRD_TEst\Products\%%G\%%H %%G.hdr)`
This did not work, it rename the files but I can not use them this way, they are not importable into ArcMap. Moving will be done in a 2nd step.
EDIT: After renaming the .hdr as well I can import the data in ArcMap finally, I updated the code-snippet above for my final solution.
Say I have a set of Subfolders, in which I have some files in them (the name of the files does not matter, nor does the file extension). How would I write a batch script to iterate over these subfolders so that I can rename all the files in them to "raw.txt". I have no idea how to approach this, I am only familiar with the "ren" command
With this command you can iterate recursively over all the files from a give directory:
for /r <rootdir> %i in (*) do #ren "%i" raw.txt
Be aware that if there is more than one file in a folder ren will not change the file's name to raw.txt. Hope it helps.
I am making a bat file which will change the extension then move it to another folder
I have managed to do the replace part like so:
ren *.txt *.jpg
However I would like to move all .jpg file without moving the .bat file that renamed them.
Thank you.
Just use move:
ren *.txt *.jpg
move *.jpg \NewDestination
Of course, you should replace \NewDestination with the actual path to the folder on your system where you want the files to be moved.
I find the TechNet Command Line Reference a useful place to have bookmarked.
you can use something like:
move dir1\*.jpg dir2\...
I want to create a .bat file that will help me to find/search a particular .exe file in folders in a particular location and then run that .exe file.
e.g
search file in particular location e.g."\G:\data" and in this prime location there is many folders and in that folders many sub folders are there. and the .exe file is available in one of these sub-folder and the location I do not know. only I know the name of the .exe file.
bat file need to search that file and then run that one.
below two are not able to find and run the file.
1st program
pushd G:\data-a\test
FOR /F "delims=" %F IN ('dir /S /b autorun.exe') DO SET ExePath="%F"
%ExePath%e
2nd program
for /r G:\data-a\test %a in (autorun.exe) do "%a"
from the above two different program I put it in to notepad for creating two different .bat file and executed one after anther file. but that was unable to locate and run autorun.exe file.
just for example my autorun.exe file exact locations are
G:\data-a\test\test1\t1 and G:\data-a\test\test1\t1
but in G:\data-a\test location there may be several subfolders.
so to run autorun.exe one by one that to find the file in subfolder is difficult. I want to give the path G:\data-a\test in program and all the files will be found in subfolder and then run automatically.
Three liner (use %%F if wrapping inside a batch file).
pushd G:\data
FOR /F "delims=" %F IN ('dir /S /b something.exe') DO SET ExePath="%F"
%ExePath%
or the one liner from the command prompt:
for /r g:\data %a in (filename.exe) do "%a"
used the quotes for %a in case the calling directory path has spaces in it
From a DOS command I want to move all files that do not match a file name pattern.
Something like this:
For example I want to move all files that do not start with "aaa"
for %i in (*) do if not %i == aaa* move %i .\..
XCOPY is designed to work with 'exclude' lists... See below:
dir /b /a-d "source"|findstr /b "aaa" >"%temp%\aaafiles.tmp"
xcopy "source" "destination\" /exclude:%temp%\aaafiles.tmp /y
The first line performs a DIR (directory) listing of the source folder, listing files in bare format (/b) ignoring directory names (/a-d). The output is piped into the FINDSTR command.
FINDSTR looks at each filename and compares it's beginning (/b) with the string "aaa".
If the start of a filename matches the string "aaa", the filename is redirected (written) to the file aaafiles.tmp in the users TEMP directory.
The /b is vital because you don't want to exclude files such as theaaafile.txt.
The XCOPY command copies files from the source folder to the destination folder except files that are listed in aaafiles.tmp.
Prompting to overwrite existing files (/y) is turned off.
source and destination will need to be replaced your own foldernames.
Robocopy is a possibility
robocopy source destination *.* /mov /XF aaa*.*
for options see here http://technet.microsoft.com/en-us/library/cc733145.aspx
If you don't mind fiddling with the archive bit, you can use it to selectively copy and delete files based on a file mask.
Move (copy and delete) all files except those beginning w/"aaa" from current directory to "dest". May also specify full source path.
attrib +a *.*
attrib -a aaa*.*
xcopy /a [or /m] *.* dest
del /aa /q *.*
One way you can do it is create a list of the files to move in a temporary file. Then use the file in with the for command. Generate the list using findstr.
> dir/b/a-d | findstr /v aaa* > "%temp%\#movelist"
> for /f %f in (%temp%\#movelist) do move %f ...
The first command gets a list of all files (with no directories) in the current directory and then pipes the list to findstr which excludes (/v) filenames that match the pattern and puts it in the file #movelist in the temp directory. The second command just takes those results so you may do what you will with them (move it).
There's probably a better way to do it in a single command without the temporary file, I just don't know how to write it. I'm not sure how to call the dir command from within the for command. AFAIK it only takes program files that exist, not builtin commands.
In some cases it can be made more simple. For example, I had to copy recursively a bunch of directories but excluding all images (png and bmp files), so I simply created an excludeList.txt file containing:
.png
.bmp
and run
xcopy /S /I <source> <dest> /exclude:c:\excludeList.txt
It will match any file or directory containing .png, but not necessarily ending by .png. (I did not investigate if smart use of wildcards or regular expressions are possible).
It does not handle your particular example (for which you have already a good answer) but it solved my problem, and this page is what I found when I googled in search of a solution :)
Not ideal, but moving all files to the destination and moving the files back to the source is a fast way with actual move operation (no copies). Of course this assumes there are no files in destination matching the wildcard.
move source\*.* destination\ && move destination\aaa*.* source\