Solved Rename hello.txt to hello2.txt using bat from desktop

March 29, 2013 at 11:30:59
Specs: Windows 7
Tried create bat file using this code but it's not working any help?

ren C:\Program Files\software\hello.txt hello2.txt

and another one can we swap the name
EX:got two files name hello.txt and hello2.txt

so if we click the .bat file it will swap the files name to

hello.txt to hello2.txt

then if we click again it will swap the file again

hello2.txt to hello.txt
hello.txt to hello2.txt

Thanks


See More: Rename hello.txt to hello2.txt using bat from desktop

Report •

✔ Best Answer
March 29, 2013 at 13:41:51
dang, I forgot the 'exit' command after the second check for hello.txt. I need to check my work better... Use this code instead:

@echo off

if exist "C:\Program Files\Software\hello.txt" goto step2
echo.
echo hello.txt is missing. Press any key to exit.
pause >nul
exit

:step2
if exist "C:\Program Files\Software\hello2.txt" goto step3
echo.
echo hello2.txt is missing. Press any key to exit.
pause >nul
exit

::If the two .txt files are found, the following code swaps their filenames.
:step3
if exist "%temp%\hello.txt" del "%temp%\hello.txt"
if exist "%temp%\hello2.txt" del "%temp%\hello2.txt"
copy /v /y "C:\Program Files\Software\hello.txt" "%temp%\hello.txt"
del /f "C:\Program Files\Software\hello.txt"
ren "%temp%\hello.txt" hello2.txt
ren "C:\Program Files\Software\hello2.txt" hello.txt
copy /v /y "%temp%\hello2.txt" "C:\Program Files\Software\hello2.txt"
if exist "%temp%\hello.txt" del "%temp%\hello.txt"
if exist "%temp%\hello2.txt" del "%temp%\hello2.txt"
echo.
echo Filename have been changed from hello.txt to hello2.txt and vice versa.
echo.
echo Press any key to close this script
pause >nul
exit



#1
March 29, 2013 at 11:42:59
Try this. It works on my machine. (Code starts with @echo off)

Start of code:
@echo off
if exist "C:\Program Files\Software\hello.txt" goto name2
if exist "C:\Program Files\Software\hello2.txt" goto name
echo.
echo.
echo NO TEXT FILE FOUND.

::If C:\Program Files\Software\hello.txt is found rename to hello2.txt
:name2
ren "C:\Program Files\Software\hello.txt" hello2.txt
echo.
echo Filename changed from hello.txt to hello2.txt
echo.
echo Press any key to close this script
pause >nul
exit


::If C:\Program Files\Software\hello2.txt is found rename to hello.txt
:name
ren "C:\Program Files\Software\hello2.txt" hello.txt
echo.
echo Filename changed from hello2.txt to hello.txt
echo Press any key to close this script
pause >nul
exit


Report •

#2
March 29, 2013 at 11:46:29
Sorry, use this one. I forgot a pause after the first section:

@echo off
if exist "C:\Program Files\Software\hello.txt" goto name2
if exist "C:\Program Files\Software\hello2.txt" goto name
echo.
echo.
echo NO TEXT FILE FOUND. Press any key to exit.
pause >nul

::If C:\Program Files\Software\hello.txt is found rename to hello2.txt
:name2
ren "C:\Program Files\Software\hello.txt" hello2.txt
echo.
echo Filename changed from hello.txt to hello2.txt
echo.
echo Press any key to close this script
pause >nul
exit


::If C:\Program Files\Software\hello2.txt is found rename to hello.txt
:name
ren "C:\Program Files\Software\hello2.txt" hello.txt
echo.
echo Filename changed from hello2.txt to hello.txt
echo Press any key to close this script
pause >nul
exit


Report •

#3
March 29, 2013 at 11:50:44
If you want it to prompt you for the filename you want to use, that can easily be achieved by creating a variable.

Report •

Related Solutions

#4
March 29, 2013 at 12:53:07
Scrip works but it can't swap the files names, i think it's not possible to swap the files name eachother right? becase it will always see the file already exist. :P

Report •

#5
March 29, 2013 at 13:03:06
I must have misunderstood; I thought you only had 1 file.

To make sure I understand: You have two files (hello.txt and hello2.txt) that currently exist in the same location in one folder. You want to be able to have the files swap names so hello.txt is now hello2.txt and vice versa? Let me know and I can come up with something slightly different to accommodate.


Report •

#6
March 29, 2013 at 13:05:03
To answer your question, I think it is very possible. I would just have the script temporarily move one of the files, rename both files, and move it back.

Depending on the size of the files, it would still be a very quick process.


Report •

#7
March 29, 2013 at 13:12:38
Yes that's what i want to do, two files in the same folder then swap eachother files names :D can i see the script if you don't mind, the files size is 14kb and 12kb in size :)

Report •

#8
March 29, 2013 at 13:21:54
@echo off

if exist "C:\Program Files\Software\hello.txt" goto step2
echo.
echo hello.txt is missing. Press any key to exit.
pause >nul
exit

:step2
if exist "C:\Program Files\Software\hello2.txt" goto step3
echo.
echo hello2.txt is missing. Press any key to exit.
pause >nul

::If C:\Program Files\Software\hello.txt is found rename to hello2.txt
:step3
if exist "%temp%\hello.txt" del "%temp%\hello.txt"
if exist "%temp%\hello2.txt" del "%temp%\hello2.txt"
copy /v /y "C:\Program Files\Software\hello.txt" "%temp%\hello.txt"
del /f "C:\Program Files\Software\hello.txt"
ren "%temp%\hello.txt" hello2.txt
ren "C:\Program Files\Software\hello2.txt" hello.txt
copy /v /y "%temp%\hello2.txt" "C:\Program Files\Software\hello2.txt"
if exist "%temp%\hello.txt" del "%temp%\hello.txt"
if exist "%temp%\hello2.txt" del "%temp%\hello2.txt"
echo.
echo Filenames changed from hello.txt to hello2.txt and vice versa.
echo.
echo Press any key to close this script
pause >nul
exit


Report •

#9
March 29, 2013 at 13:22:59
The script I posted above works like a charm on my machine. Let me know if you have any issues.

Report •

#10
March 29, 2013 at 13:41:01
Works great but can i know where it creat the temp file? in C:\Program Files\Software\ ?
and 1 more thing can i put this code somewhere in the acript to run this .exe file?

rundll32 shell32.dll,ShellExec_RunDLL "C:\Program Files\Software\run.exe"

Thanks script really awesome :D


Report •

#11
March 29, 2013 at 13:41:51
✔ Best Answer
dang, I forgot the 'exit' command after the second check for hello.txt. I need to check my work better... Use this code instead:

@echo off

if exist "C:\Program Files\Software\hello.txt" goto step2
echo.
echo hello.txt is missing. Press any key to exit.
pause >nul
exit

:step2
if exist "C:\Program Files\Software\hello2.txt" goto step3
echo.
echo hello2.txt is missing. Press any key to exit.
pause >nul
exit

::If the two .txt files are found, the following code swaps their filenames.
:step3
if exist "%temp%\hello.txt" del "%temp%\hello.txt"
if exist "%temp%\hello2.txt" del "%temp%\hello2.txt"
copy /v /y "C:\Program Files\Software\hello.txt" "%temp%\hello.txt"
del /f "C:\Program Files\Software\hello.txt"
ren "%temp%\hello.txt" hello2.txt
ren "C:\Program Files\Software\hello2.txt" hello.txt
copy /v /y "%temp%\hello2.txt" "C:\Program Files\Software\hello2.txt"
if exist "%temp%\hello.txt" del "%temp%\hello.txt"
if exist "%temp%\hello2.txt" del "%temp%\hello2.txt"
echo.
echo Filename have been changed from hello.txt to hello2.txt and vice versa.
echo.
echo Press any key to close this script
pause >nul
exit


Report •

#12
March 29, 2013 at 13:45:17
It's just copying the file to your temp folder then deleting it afterward.

I don't know 100% what you're refering to when you say run it as a .exe. but you can call this script from a .exe or from a .vbs.


Report •

#13
March 29, 2013 at 13:50:53
Thanks for the updated script,

No problems i put this code at the second line of the code and it works fine :D

rundll32 shell32.dll,ShellExec_RunDLL "C:\Program Files\Software\run.exe"

Oh so in Windows 7 the Temp file will be at C:\Users\hashp\AppData\Local\Temp right?

Thanks


Report •

#14
March 29, 2013 at 13:53:17
Correct!

Glad I can help!


Report •

#15
March 29, 2013 at 14:10:15
Thank you very much for the help :D

Report •

Ask Question