| 1. |
Solve : batch file for renaming existing folder? |
|
Answer» My requirement is : If folder is already exist then do not replace it and create new folder with siffix. Thanks, it is working. how to write else part, i triedCode: [SELECT]IF EXIST "C:\users\Squash\My Folder" ( md "C:\users\Squash\My Folder_XXXXX" ) else ( md "C:\users\Squash\My Folder" )not working. for if section it says -- the syntax of command is incorrect and for else section it says -- 'else' is not recognised as an external and internal command please advise.You need the trailing \ to reliably test for folders, but the syntax looks fine and works fine here in Windows 7. What OS are you using? IF EXIST "C:\users\Squash\My Folder\" ( md "C:\users\Squash\My Folder_XXXXX" ) else ( md "C:\users\Squash\My Folder" ) Forgive my INTRUSION, but wouldn't it save space to write: Code: [Select]IF EXIST "C:\users\Squash\My Folder\" md "C:\users\Squash\My Folder_XXXXX" IF NOT EXIST "C:\users\Squash\My Folder\ md "C:\users\Squash\My Folder" That way you use 2 lines instead of 5. That is, unless there is a flaw in this method that I didn't see. Unless you plan on ADDING and rd to the if exist statement, which would require and else.I beat you with one line. Code: [Select]IF EXIST "C:\users\Squash\My Folder\" (md "C:\users\Squash\My Folder_XXXXX") else (md "C:\users\Squash\My Folder")Quote from: foxidrive on August 14, 2012, 01:26:18 AM I beat you with one line. You also won in the character count event. However, the double-liner was easier to read. By the time I got to the end of the one-liner, I was gasping for air and forgotten how I got got there. Quote from: Sidewinder on August 14, 2012, 05:57:45 AM You also won in the character count event. However, the double-liner was easier to read. By the time I got to the end of the one-liner, I was gasping for air and forgotten how I got got there.Which is why I break it up. Easier to read.This is shorter, and a single line, even with a couple of whitespace characters. Code: [Select]MD "C:\users\Squash\My Folder\" 2>nul || md "C:\users\Squash\My Folder_XXXXX"Quote from: foxidrive on August 14, 2012, 06:42:03 PM This is shorter, and a single line, even with a couple of whitespace characters.Me likey! Conditional Operators are one of my favorite things about batch |
|