-1

I know there are many examples of this but I just can't get it and this shouldn't be THAT difficult but I am finding it to be.

I have a folder with 4 files in it:

rtcc_current_pnp_data1-2018-12-26-061000.txt
rtcc_current_pnp_data2-2018-12-26-061000.txt
rtcc_current_pnp_data3-2018-12-26-061000.txt
rtcc_current_pnp_data4-2018-12-26-061000.txt

the date/time at the end changes daily.

Each day I want to run a batch file to rename these to:

data1.txt 
data2.txt
data3.txt 
data4.txt

That's it...can someone please help me extract the correct name from the files and rename them?

So I tried this and it doesn't do anything:

for %%A in (\\apdwhdb01\D$\ProbationImportFolder\Current\rtcc*.*) do (
  set "filename=%%A"
  set "newName=EXP_!filename:~18!"

  rem ** remove the ECHO when it seems to work
  ren !filename! !newName!
)
Leslie
  • 3,604
  • 7
  • 38
  • 53
  • As you say, there are many examples, please read them, create your code, then come back here to [edit your question](https://stackoverflow.com/posts/53937620/edit), if the code you've written doesn't perform as written and intended. You would then post a [mcve] of the code after, having followed the advice provided in [ask]. _Technically this topic should be closed as a duplicate, so I'd suggest that you consider my advice sooner rather than later._ – Compo Dec 26 '18 at 22:10
  • I also tried this: https://www.experts-exchange.com/questions/28127794/Batch-script-to-parse-a-date-out-of-a-filename.html https://stackoverflow.com/questions/19140044/renaming-files-using-batch-file https://www.robvanderwoude.com/battech_rename.php I apologize for not providing more details but I have been searching all day for this and am very frustrated – Leslie Dec 26 '18 at 22:19
  • It's your responsibility to put together some code from the examples you've found, not ours. Whilst I appreciate that it's not always a simple task to piece together code when it's not familiar to you, it is both courteous to the Membership and the Site itself, to show some effort. – Compo Dec 26 '18 at 22:47
  • I have tried to put together code from the examples and have failed - that is why I asked for help. Part of the reason I am struggling is because I can't find any explanation of what all those commands do. I don't even know what terms to search for to figure out what this is doing @echo off pushd "pathToYourFolder" || exit /b for /f "eol=: delims=" %%F in ('dir /b /a-d *_*.jpg') do ( for /f "tokens=1* eol=_ delims=_" %%A in ("%%~nF") do ren "%%F" "%%~nB_%%A%%~xF" ) popd from this post: https://stackoverflow.com/questions/14013145/windows-batch-file-rename – Leslie Dec 27 '18 at 01:56
  • To find out what a command does, open a Command Prompt window and enter the command with the question mark option, _e.g. `For /?`, `Set /?`, `Ren /?`..._. – Compo Dec 27 '18 at 08:43
  • `I can't find any explanation of what all those commands do`: You should bookmark [SS64])(https://ss64.com/nt/) – Stephan Dec 27 '18 at 09:41

3 Answers3

1

The easiest way is with this:

 ren "\\apdwhdb01\D$\ProbationImportFolder\Current\*data1*.txt" data1.txt
 ren "\\apdwhdb01\D$\ProbationImportFolder\Current\*data2*.txt" data2.txt
 ren "\\apdwhdb01\D$\ProbationImportFolder\Current\*data3*.txt" data3.txt
 ren "\\apdwhdb01\D$\ProbationImportFolder\Current\*data4*.txt" data4.txt

However, the problem you are probably facing is that the network connection isn't active/valid for your connection. The easiest way to find out is with this:

 dir "\\apdwhdb01\D$\ProbationImportFolder\Current"

If it returns your file list then you're okay. If not then you'll want to map the drive first in the same batch file:

 :: temporarily map the network share to the Q: drive.
 :: make sure you update the username and password
 net use q: "\\apdwhdb01\D$\ProbationImportFolder\Current" /user:MyUserName myPassword

 :: rename the files
 ren "q:\*data1*.txt" data1.txt
 ren "q:\*data2*.txt" data2.txt
 ren "q:\*data3*.txt" data3.txt
 ren "q:\*data4*.txt" data4.txt

 :: remove the mapped drive
 net use q: /delete
shawn
  • 383
  • 2
  • 8
0
@echo off

pushd "\\apdwhdb01\D$\ProbationImportFolder\Current" || exit /b 1

for %%A in (data1 data2 data3 data4) do (
    for %%B in ("rtcc_current_pnp_%%~A-20*.txt") do (
        ren "%%~B" "%%~A.txt"
    )
)

popd

The pushd will map the network share to a temporary mapped drive. The || is if the command on the left fails, do the command on the right, which is exit /b 1 that ends the script.

The for loop iterates through data1, data2, data3 and data4.

The nested for loop iterates the file names using %%A with a value of i.e. data1, and uses a wildcard for the datestamp variation. %~B is the current filename which is renamed to %%~A.txt, in which %%~A holds the current value of i.e data1. The ~ in %%~A is a modifier which will remove surrounding double quotes.

The popd will retore the previous current working directory that pushd changed. It should remove the temporary mapped drive.

To get help on a command, use i.e. pushd /?, which will print out help information. for /? prints help information about for, ren /? ... etc.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
  • Thank you! I will check this out tomorrow when I get to work. I appreciate your explanation and feel that I will actually learn something. – Leslie Dec 27 '18 at 02:58
0

A two line script example:

@PushD "\\apdwhdb01\D$\ProbationImportFolder\Current" 2>Nul || Exit /B
@For %%A In ("rtcc_*.txt") Do @For /F "Tokens=4 Delims=_-" %%B In ("%%A") Do @Ren "%%A" "EXP_%%B%%~xA"

For information on the commands used, open a Command Prompt and enter each of:

PushD /?
Exit /?
For /?
Ren /?

Compo
  • 36,585
  • 5
  • 27
  • 39