Suppressing REN errors in Windows batch file - batch-file

I'm testing my batch file which renames files based on tab-delimited text file pairs:
OldName [tab] NewName
I'm now being rude to my script and testing scenarios with duplicate name attempts, obviously getting error messages A duplicate file name exists, or the file cannot be found.
The question is: can I suppress them and only display a summary at the end of the script? I tried:
ren "%%H." "%%I." >nul
but to no avail.

>nul will write STDOUT ("Stream 1") to nirvana. "Normal" messages will use STDOUT. (>nul is a short form for 1>nul
Errormessages are written to STDERR ("Stream 2"). To suppress them, use 2>nul
If you want a summary, dont write it to nul, but to a file and type the file at the end of your script.
Use >> to append to a file, instead of overwriting it (>)
ren OldName.ext NewName.ext 2>>error.log
REM ... more of your script
echo done. Errors are:
type error.log

Related

How can one use a rename batch file and maintain original file's extension but also has periods in the new file name?

I have a group of several documents where I am needing to rename files to new filenames with multiple periods in them. There are various types of file extensions.
Example:
REN "ABC123.*" "new.filename.here.*"
This won't function, as the multiple periods in the new filename confuses the system and it doesn't understand where the file extension should actually be. This ends up renaming it to new.filename.here in error.
The output in the above example (if ABC123 was a PDF file) should end up as new.filename.here.PDF. If ABC123 was an XLSX file, then it should end up as new.filename.here.XLSX
Any ideas as to accomplishing this?
If you want to perform this action directly from the prompt, try
for %e in (abc123*) do echo ren "%e" "new.filename.here%~xe"
But for convenience, create a file named something like chgname.bat (absolutely not ren.bat or rename.bat) with the following content:
#ECHO OFF
for %%e in ("%~1*") do echo ren "%%e" "%~2%%~xe"
GOTO :EOF
If you locate this filename in any directory located on the path (see PATH from the prompt - it's a semicolon-separated list of directories examined by windows when locating an executable that is not found in the current directory) then
chgname abc123 new.filename.here
will change those names for you.
Noting : BOTH OF THESE METHODS will simply echo the ren command for verification. Change echo ren to ren to activate the rename.
I've included no safety measures in the batch file such that it may resist invalid input.
With the batch version, executing chgname "abc123" "new.file with a space name.here" could be used to change abc123* to new.file with a space name.here* - the quotes allow for spaces in names.

How can i make a batch file test if a file can be renamed?

I need someone to help me make a bacth file that can test if a file can be renamed and then open a info box telling if the file can be renamed or not..
The problem in all of this is that I use interactive pdf files, and if two people on my network open the file at the same time, none of them can save the changes that they make in the file. But I know that if someone has the file open it can't be renamed, so that is why I need a batch file that can test if a file can be renamed without renaming it. If it can be renamed I need it to open a popup box saying "The file is ready for editing" and if it can't be renamed I need the popup box to say "The file is in use, please try again later"
I'll be very happy if ther is someone who is will to take on this challange :)
Best Regards
Dion
#echo off
ren "file.pdf" "file.pdf" 2>nul || (
echo Sorry, the file is in use.
echo Please try again later.
else start "C:\Test" file.pdf
)
You have almost answered your own question, without realizing it :-)
Simply attempt to rename a file to the same name. If it succeeds, then the file is not locked and there is no harm. If it fails, then you know the file is locked.
For example:
#echo off
ren "yourFile.pdf" "yourFile.pdf" 2>nul || (
echo Sorry, file is locked by another user
rem Take some error action, perhaps GOTO or EXIT /B
)
rem Now open the pdf file
But beware - this strategy is not foolproof because you have a race condition. If two processes run against the same file at nearly the same time, then they both may "rename" the file successfully before either has a chance to open the file.
try this code:
ren "pdffile.pdf" "renameto.pdf"
if %errorlevel% NEQ 0 (
echo The file cannot be renamed.
rem The following line makes a dialog box saying that an error happened:
echo msgbox "The file could not be renamed.">"%TEMP%\msg.vbs" && cscript /nologo "%TEMP%\msg.vbs" && del "%TEMP%\msg.vbs"
rem put other commands here
)

How to create a script that creates text files in every folder in a directory? (Windows Batch)

So basically, let's say I have the following folders in a directory:
test_folder_1
test_folder_2
And I also have a text file with the list of all the folders in that directory.
How can I create a script that will create a text file, test.txt, in all the folders in the directory? (test_folder_1 and test_folder_2)?
I tried modifying some code I found online and got this:
for /F "tokens=1 delims=," %%v IN (folderlist.txt) DO echo test>"C:\Users\myname\My Documents\test\"%%v""
However, running this, I get "Access Denied."
Any idea what went wrong, or alternative ways to do this? Thanks.
The command
echo test>"C:\Users\myname\My Documents\test\"%%v""
despite the wrong double quotes around %%v redirects the text test directly to the directory instead of a file test.txt in the directory. Or in other words: The attempt to create a file with name of an already existing directory is denied by Windows and the result is the access denied error message.
The solution for creating a file test.txt with the line test in each directory specified in text file folderlist.txt is:
for /F "tokens=1 delims=," %%v in (folderlist.txt) do echo test>"%USERPROFILE%\My Documents\test\%%v\test.txt"
It is also possible to create a file test.txt with a file size of 0 bytes in all subfolders of the test folder with following command line in the batch file.
for /D %%v in ("%USERPROFILE%\My Documents\test\*") do echo. >nul 2>"%%v\test.txt"
The command FOR returns in this case the subfolder name with full path always without surrounding double quotes even if the folder path contains a space character as it is the case here.
See How to create empty text file from a batch file? and read the Microsoft article about Using command redirection operators for more details.
echo. >nul 2>test.txt results in writing just a new line to STDOUT redirected to device NUL to avoid printing lots of blank lines on batch execution to screen. And to the file text.txt nothing is redirected from STDERR as there is no error message printed by command ECHO resulting in creating an empty test.txt file.
Run in a command prompt window for /? for help on command FOR.

How to detect using microsoft .bat files is file empty?

and exectute some command if file empty
Here's a sample batch file that does this. You call it with the name of the file you want to check is empty or not:
#ECHO OFF
IF "%~z1"=="0" GOTO FileEmpty
ECHO File is not empty
GOTO End
:FileEmpty
ECHO File is empty
:End
Note that the file must exist in order for this to work. But you can check that easily with IF EXIST

Is it possible for a batch file to create a file, but only if the file isn't already there?

I would like to know if its possible for a batch file to create a file in a certain location, but only if it doesn't exist there.
For example, if the file in question doesn't exist, create it. Otherwise, do nothing.
Use "if":
if not exist c:\test\test.txt type NUL > c:\test\test.txt
echo 2>> c:\test\test.txt
If echo ever writes anything to the error stream you'll get a shock, but I can't think of any circumstance where it would...
if not exist C:\test\test.txt copy NUL C:\test\test.txt
(And it's really hard typing those paths. I think I never touched anything below the drive root for a few years by now. Are you creating files on Unix systems in / out of habit too?)
if not exist C:\test\test.txt echo (what you want to be inside of the file here) >>C:\test\test.txt
Here's how to create a file with a batch file:
if not exist "C:\folder\test file.txt" goto create
exit
:create
echo Your>"C:\folder\test file.txt"
echo Text>>"C:\folder\test file.txt"
echo Here>>"C:\folder\test file.txt"
If you want you can replace exit with code for handling the existing file.
If the file exists, nothing will happen.
If the file doesn't exist, a file will be created with the text:
Your
Text
Here
It works. I tested it.

Resources