#1 22 Jul 2019 08:14

Rekrul
Member
Registered: 17 Apr 2016
Posts: 32

I did something stupid!!! Did I screw anything up???

So I'm working on a script and I forgot that PATH is the system variable used to tell it where to find commands, and I defined it in my script according to the most recent filename. This caused the script to fail because it could no longer find the external command I used.

I changed the variable name to filepath instead, but I forgot to change a rename command later in the script that used the variable name path. So the script executed;

ren "!path!!name1!.txt" "!name2!.txt"

With the path variable set to the system command path, which goes to all the default dirs, plus a few I've added.

This generated the error;

"A duplicate file name exists, or the file cannot be found"

Now, logic says that the command failed because it couldn't find the filename that the script gave it, but logic has very little place in scripts. The part that worries me is the "A duplicate file name exists..." message. Normally when you try to rename a non-existent file, you get "The system cannot find the file specified."

I've checked all the directories in the path and nothing seems out of place and nothing has broken yet, but I still can't help worrying.

Would executing a rename command with the path variable like that do anything? Or did it just completely fail?

I've tried to manually duplicate this using test dirs and entering a similar command on the command line, but it just tells me that the syntax is incorrect

Offline

#2 22 Jul 2019 09:19

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,090

Re: I did something stupid!!! Did I screw anything up???

Assuming your PATH variable contained your proper path info, I assume it's going to have done something like

ren "C:\Foo;D:\bar;C:\windows\baz;!name1!.txt" "!name2!.txt"

Which is probably fine.

You could maybe run that part of the script again with the error in it but change that line to

echo ren "!path!!name1!.txt" "!name2!.txt"

But I don't know what else the script is doing to say whether THAT is a safe thing to do or not, or if you've already changed the state of something that might mean a re-run is not indicative.


cmd | *sh | Ruby | Chef

Offline

#3 22 Jul 2019 10:22

Rekrul
Member
Registered: 17 Apr 2016
Posts: 32

Re: I did something stupid!!! Did I screw anything up???

bluesxman wrote:

Assuming your PATH variable contained your proper path info, I assume it's going to have done something like

ren "C:\Foo;D:\bar;C:\windows\baz;!name1!.txt" "!name2!.txt"

Which is probably fine.

Here is my Path variable;

C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\MinGW\bin;C:\Commands;C:\Program Files\mkvtoolnix7.0.0.7\;C:\Program Files\NVIDIA Corporation\PhysX\Common

"C:\Commands" is where I dump all the small command line programs that don't warrant their own directory in Program Files, like fsum.exe (checksum utility), unecm.exe (ISO uncompressor), etc. I wanted them to be available from the command line, but I didn't want to clutter up the Windows directory or any other dir with them, so I gave them their own dir and added it to the path. EDIT: Also, putting them all in one directory avoids having to add a ton of extra paths to PATH.

bluesxman wrote:

You could maybe run that part of the script again with the error in it but change that line to

OK, I did that and it generated the following;

ren "C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\MinGW\bin;C:\Program Files\mkvtoolnix7.0.0.7\;C:\Program Files\NVIDIA Corporation\PhysX\Commontumblr_ljglodm75h1Wbhzs2o1_400.txt" "tumblr_ljglodm75h1Wbhzs2o1_500.txt"

The purpose of this part of the script is to rename any text files that might accompany the picture file, but have the wrong size in the filename. I'm trying to make my script as foolproof as possible when correcting image filenames. It's rare, but sometimes I'll make a note, or clip some text from the web site when I save an image and I'll save it with the same filename as the picture. Since I'm correcting the sizes in Tumblr filenames and renaming the files, I also want to correct any associated text file names at the same time.

In other words, I may have;

tumblr_ljglodm75h1Wbhzs2o1_400.jpg
tumblr_ljglodm75h1Wbhzs2o1_400.txt

When it should be labeled as "_500", so my script renames the picture, then tries to rename the text file if it exists, so that I end up with;

tumblr_ljglodm75h1Wbhzs2o1_500.jpg
tumblr_ljglodm75h1Wbhzs2o1_500.txt

bluesxman wrote:

But I don't know what else the script is doing to say whether THAT is a safe thing to do or not, or if you've already changed the state of something that might mean a re-run is not indicative.

The only other destructive thing it does is to rename the picture file, if needed. That part never included the PATH variable.

The actual line was;

if "!rename!"=="!tumblr!" ren "!path!!name1!.txt" "!name2!.txt" 2>nul

I set a flag if the file needs to be renamed, and a flag if it's a Tumblr file. If both flags are set (I made sure to set the Tumblr one to something other than 0 at the start so that it wouldn't always match) then the file being renamed is a Tumblr file and it needs to try renaming a text file too. I suppose I should have checked for the existence of said text file, but I didn't think of it until just now. It doesn't need to rename any non-Tumblr text files because those image files will only have their extensions changed (if necessary) and the name of the text file will still match.

I'm using a variable for the path, because my script recurses into directories and just using the filename will fail, since the path isn't included. I tried using %%~pF in the rename command, but that didn't work. Neither did %%~pnF to get the path and the filename without the extension.

I'm also confused because I used 2>nul to suppress error messages in case the file didn't exist, but I still saw the message about duplicate filenames.

Last edited by Rekrul (22 Jul 2019 11:26)

Offline

#4 22 Jul 2019 11:34

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,090

Re: I did something stupid!!! Did I screw anything up???

I think you should be OK here.  This looks like it would be destined to fail due to being an invalid file name (thanks to all the C:)

ren "C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\MinGW\bin;C:\Program Files\mkvtoolnix7.0.0.7\;C:\Program Files\NVIDIA Corporation\PhysX\Commontumblr_ljglodm75h1Wbhzs2o1_400.txt" "tumblr_ljglodm75h1Wbhzs2o1_500.txt"

As for the error displaying -- you're only suppressing &2, maybe "REN" misbehaves and sends that error (or all errors) to &1

Last edited by bluesxman (22 Jul 2019 13:04)


cmd | *sh | Ruby | Chef

Offline

Board footer

Powered by FluxBB