Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Consider this script:

SETLOCAL EnableDelayedExpansion
for /F "tokens=2 delims=." %%G in ("%1") do set partial_name = %%G
 if partial_name GEQ 9200 ( move I:\rosa\transfertmp\%1  L:\OSLDos\Tmp\ ) else  ( move I:\rosa\transfertmp\%1 L:\LdisOSLDos\Tmp\ ) 
ENDLOCAL

invoked as thisscript 11111.9100.csv

What I want to do is: move the file to the correct folder, based on the second segment of the file name (9100), so if it is greater than 9200 move it to one folder and if it is not, move to another folder.

The problem seems to that the comparison does not work.

share|improve this question
add comment

2 Answers

Dennis identified one problem and provided a solution in his answer. But there are still other issues.

1) Your code set partial_name = %%G will include a trailing space in the variable name and a leading space in the value. The code should read set partial_name=%%G

2) Your %1 value may already be quoted, in which case you should remove the quotes before adding your own by using "%~1" instead of "%1".

3) Perhaps not a problem in your case, but it is generally a good idea to enclose file paths in quotes in case the name contains space or special characters.

4) I'm assuming you want to do a numeric comparison. The IF command uses almost the same rules as the SET /A command when it attempts to parse a number - Any number prefixed with 0 will be treated as hexadecimal notation. If it finds an invalid hex digit like 8 or 9, then the IF will revert to a string comparison instead of a numeric comparison.

For example if 09 lss 8 (echo true) else echo false returns true.

So if the 2nd segment of your file name has the potential to have leading zeros, then you need to do more work.

set "partial_name="
for /f "tokens=2 delims=." %%G in ("%~1") do (
  for /f "tokens=* delims=0" %%N in ("%%G") do set /a "partial_name=%%N"
  if not defined partial_name set "partial_name=0"
)
if %partial_name% GEQ 9200 (
  move "I:\rosa\transfertmp\%1" "L:\OSLDos\Tmp\"
) else (
  move "I:\rosa\transfertmp\%1" "L:\LdisOSLDos\Tmp\"
)
share|improve this answer
    
dbenham,Thank you indeed for this brilliant solution! It worked immediately. /Rolf –  user192750 Jan 25 '13 at 14:29
add comment

You're actually comparing the string partial_name to 9100, not the content of the variable.

Since p GEQ 9, this will evaluate to true.

Also, set partial_name = %%G sets partial_name  to  %%G (note trailing and leading spaces).

To fix both issues this, surround the variable name with percent signs (%) and remove the spaces surround the eual sign (=):

for /F "tokens=2 delims=." %%G in ("%1") do set partial_name=%%G
 if %partial_name% GEQ 9200 ( move I:\rosa\transfertmp\%1  L:\OSLDos\Tmp\ ) else  ( move I:\rosa\transfertmp\%1 L:\LdisOSLDos\Tmp\ )
share|improve this answer
    
You have identified and fixed one problem, but there are more :-) See my answer –  dbenham Jan 25 '13 at 12:52
    
You're right. It won't work with the spaces. –  Dennis Jan 25 '13 at 13:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.