I do things the old fashiohed way. I'm trying to write a DOS-type batch (.bat) program that moves all files in folder F:\IMAGES to a subfolder F:\IMAGES\RECYCLE where the following is true: xxxxxxxx.ORF exists in folder X but xxxxxxx.JPG does not.
I'm on Windows XP. Any ideas? Or maybe point me to a good resouce?
Not sure I completely understand what you want to do. This command should move xxx.jpg to another folder if there is a xxx.orf file in the same folder.
for %%f in (*.jpg) do if exist %~nf.orf move %f .\recycle
gys is close, but I think the logic is a bit sideways from what he's asking for. He wants to move file.orf if the corresponding file.jpg does NOT exist.
First, my standard disclaimer. Friends don't let friends write .bat. That's as bad, if not worse than csh. Can I interest you in a nice installation of cygwin, perhaps? ksh (or even sh/bash/etc.) for this is much, much prettier.
I assume when you mention "folder X" that you expect this to act recursively for all files/dirs under the "images" directory, and the test for file existence should only check the *same* directory as the .orf file we are iterating over.
---
@echo off
for /f %%F in ('dir /a:-d /b /s f:\images\*.orf') do call:checkFile %%F
:checkFile
if exist %~1 if not exist %~d1%~p1%~n1.jpg move %~1 f:\images\recycle\
goto:eof
---
Hardly anyone ever mentions it, but there is a better alternative for scripting in the Windows environment. It's called Powershell, and it is a standard feature in Win7, but also can be installed as an option in Vista and XP (for XP, SP2 must be installed at minimum, and .net 2.0 or later is required.)
What you want to do can, however be accomplished in a DOS batch file. I see that others have already posted better references to the DOS command syntax than I provided, so edited this.
Build a test folder with a few files, and experiment on that folder. Keep a copy of the folder elsewhere so you can restore it if your batch file does something unexpected. I'm always *very* careful about using batch files that move or delete files, especially when using wildcards.
"Exist" is what I was looking for. This gives me enough to get started. Thanks also for the helpful link and starter scripts. I'll break the DOS habit one day...
I'll second cygwin, and/or a "real" scripting language like perl, python, etc. By real, I guess I mean portable - you might be able to use it again whether in Windows, Linux, or on a Mac.
---
@echo off
for /f %%F in ('dir /a:-d /b /s f:\images\*.orf') do call:checkFile %%F
:checkFile
if exist %~1 if not exist %~d1%~p1%~n1.jpg move %~1 f:\images\recycle\
goto:eof
---
OMG, a GOTO, an actual GOTO! Someone should photograph this for unusual objects ;-)
I'm looking for a conditional move ( from F:\IMAGES to a subfolder F:\IMAGES\RECYCLE ). For instance, I might have a folder directory that looks like this.
a.orf
a.JPG
b.orf
c.orf
d.orf
d.JPG
e.orf
e.JPG
Since b.orf and c.orf have no corresponding .JPG, I only want to move those.
bvy:
Here is a script.
# Script condmove.bat
# Collect a list *.ORF files in "F:\IMAGES".
var str list ; lf -n "*.ORF" "F:\IMAGES" > $list
# Process files one by one.
while ($list <> "")
do
# Get the next file from the list.
var str orffile ; lex "1" $list > $orffile
# The file name ends in .ORF.
# Create a corresponding name .JPG.
var str jpgfile
sal -c -p "^.ORF^l" ".JPG" $orffile > $jpgfile
# Does the jpf file exist ?
af $jpgfile > null
if ( NOT ($fexists) )
# Corresponding .JPG file does not exist. Move.
system move $orffile "F:\IMAGES\RECYCLE"
endif
done
The script is in biterscripting (I added enough comments so you can translate it into batch or another language.) To run, save the script as C:\Scripts\condmove.bat, start biterscripting, enter the following command.