-2

I need to make windows batch script to rename files that have random names. I have a folder with thousand .txt files, their names are completely random, I want to rename first 5 files in that folder to file1.txt, file2.txt,file3.txt, file4.txt,file5.txt.

Help appreciated.

Jim8645
  • 181
  • 3
  • 15
  • 1
    Please take the [tour] and learn [ask]. Also your question raises more questions than answers, if all of the txt files in that folder are randomly named, how does the script know which the first five are? How does it know whether those five files have not already been renamed? I would suggest you update your question once you are better equipped to ask it properly and have actually provided code upon which to base that question. – Compo Oct 09 '16 at 13:54
  • I know how to ask, and already supplied all information needed. I already made a research, what I find was only opposite - "how to rename to random names", not this kind of problem. Actually it doesn't, it doesn't matter which files are renamed, all it need is to rename 5 files in that dir, which are then used in another application, and later deleted. I stated my problem clearly, for someone who knows and want to help. – Jim8645 Oct 09 '16 at 14:29
  • Check if [this answer](http://stackoverflow.com/a/24599740/3439404) could help using `rename "%Source%\%%~nxH" "File%%G"` instead of `move …` – JosefZ Oct 09 '16 at 15:20
  • @Compo, I didn't ask specifically you to help! You answers are pointless which I explained. When I have a starting point I provide it, like in my previous questions, when I dont have from where to start I ask and if someone knows and wants to help. – Jim8645 Oct 09 '16 at 16:01
  • I tried to follow above example: `set Source=C:\text set Target=C:\text set MaxLimit=5 rename "%Source%\%%~nxH" "File%%G" pause` I have this error: `C:\text>rename "C:\text\%~nxH" "File%G" The system cannot find the file specified.` – Jim8645 Oct 09 '16 at 16:02
  • @Mofi below your answer is little x, You should use it, what I wrote to compo goes to you, I will not waste time anymore. I didn't asked for GUI app. which I already have. Dont answer what wasn't asked! I needed additional, simpler way to do this task, if I don't find it, it doesn't hurt me. – Jim8645 Oct 09 '16 at 17:16

1 Answers1

0

Is this okay?

@ECHO OFF
(SET f=C:\Test)
IF /I "%CD%" NEQ "%f%" PUSHD "%f%" 2>NUL||EXIT/B
SET "i=5"
FOR %%A IN (*.txt) DO CALL :SUB "%%A"
EXIT/B
    :SUB
    IF %i% GTR 0 REN %1 File%i%.txt
    SET/A i-=1

Just change line two if your stated directory path\name has changed.

Compo
  • 36,585
  • 5
  • 27
  • 39