0

What I want to do:

I'm basically trying to have inside of my command something along the lines of: "if haha.txt exists, rename to lol.txt". However, there are going to be many haha.txt files in many different directories, and in order for it to work, I need to be able to run this command, and have it only rename the haha.txt file in my current directory. However, in all my attempts, it always seems to want me to specify exactly where the file is located, which won't work. (Perhaps it's because the command is in the path, not this folder?)

So the question is:

Is there any way to use the RENAME/REN command in a batch file without specifying the directory, and instead have it rename in your current directory?

Edit: As requested, here's my script (with all the stuff not important to us taken out of it)

Also, some other info:

  • Name: SSHCrack.cmd
  • Opened by: Opening command prompt, going to a folder with uncrackedSSH.txt, and typing SSHCrack 22
  • Purpose: Renaming uncrackedSSH.txt to crackedSSH.txt
  • Located in: Path

Script:

@echo off
title SSHCrack
set x=%1

if not defined x goto unknown

if %x%==22 (
goto correct
) else (
goto wrong
)

:correct
echo SSHCrack running...
if exist uncrackedSSH.txt (
ren uncrackedSSH.txt crackedSSH.txt
echo -- SSHCrack Complete --
goto end
)

if exist crackedSSH.txt (
echo SecureShell port already open
goto end
) else (
echo SecureShell port not found
goto end
)
:unknown
echo Please specify a port number
goto end
:wrong
echo Wrong port number
:end
title cmd.exe

I've tried using "ren .\uncrackedSSH.txt crackedSSH.txt", using "cd %cd%" to link to the current directory, tried directly linking with a specific folder with "cd /d C:\SpecificFolder", and tried putting the .bat file in the same folder as uncrackedSSH.txt and running it from there, but I always end up receiving:

SSHCrack Running...
The system cannot find the file specified.
-- SSHCrack Complete --

Whenever I go to the folder, the name of uncrackedSSH.txt hasn't changed.

  • 5
    It seems like something ambiguous is going wrong in your script, so it would very much help if you post your script too, and explain how you launch the script. (for example double click on it from explorer) – LPChip Nov 8 '17 at 16:05
  • Does it actually show other information too? Because it looks like it doesn't find the batch file itself rather than executing it. If that's the case, then your path is wrong or the batch file is in the wrong location. – LPChip Nov 8 '17 at 17:23
  • Yeah, sorry, it also shows the echos, and I have a few other things in that PATH folder that work, so it's definitely executing the file. – Jarren Nov 8 '17 at 17:28
  • if you add the command dir in front of the renaming and run the script again, do you see the txt file? Also, make sure there are no typo's. – LPChip Nov 10 '17 at 7:28
1

Batchfiles will only rename files that are in the current folder.

So a simple:

ren haha.txt lol.txt

is enough. No need to specify the folder.

If you get an error that it can't find the haha.txt, then make sure the path is correct. You can navigate to the correct folder first and rename then. For example:

cd /d c:\temp
ren haha.txt lol.txt

So if the batch file is not in c:\temp, you need to either use the cd command, or if you place the batch file in a folder that is part of the path environmental variable, such as c:\windows\system32, you can launch a command prompt, perform the cd part yourself, then just type the name of the batchfile which has the content of the first example in this answer.

EDIT: Based on the new provided information, it seems that your batchfile is not found, not an error from the renaming. If you copy the batchfile directly into the folder, and launch it then, does it work? This is not a permanent fix, just a step to cofirm that the batchfile is located in the wrong folder.

|improve this answer|||||
  • Might be a link (LNK) to the batch file. In that case, specify the directory at the lnk file – Thomas Weller Nov 8 '17 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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