1

I am trying to move batch files to their respective folder, they have similar names expect the batch files I want to move have "Build" on the end. For example:

Folder name: wld-vine-tstrm

Bat file name: wld-vine-tstrmBuild.bat

In the same parent directory, I also have batch files with identical names to the folder, I do not want these to move, only the ones with "Build" at the end of the file names.

setlocal enabledelayedexpansion
set folderpath=E:\Build\ModelBatches\world-wild\actor50
for %%f in (%folderpath%\*.bat) do (
  set "foldername=%%~nf"
  move "%%f" "!foldername:~0,-5!"
)

This is very close to the result I am looking for; it moves the "Build" batches to the respective folder, but it also removes the last 5 characters from the batches I dont want to move.

Correctly moved batch files

The below batch files are now missing the last 5 chars, "tstrm"

Renamed batch file

To summarize, There are two batch files for each folder in a directory, I only want to move the one ending "Build" to its folder.

smasher248
  • 23
  • 6
  • Does this answer your question? [batch copy/move files to folders with same name](https://stackoverflow.com/questions/34453880/batch-copy-move-files-to-folders-with-same-name) – T3RR0R Dec 27 '19 at 16:54
  • 6
    Why don't you use `*Build.bat` instead of `*.bat` then? – Compo Dec 27 '19 at 17:01
  • That worked, not sure how I missed it. Thanks. – smasher248 Dec 27 '19 at 17:25
  • Change the `move` command line to `move "%%~f" "!foldername:~0,-5!\"` (note the trailing backslash), so you force the destination to be interpreted as a folder and avoid files to become renamed... – aschipfl Dec 28 '19 at 11:48

1 Answers1

0

You should use this:

setlocal enabledelayedexpansion 
set folderpath=E:\Build\ModelBatches\world-wild\actor50 
for %%f in (%folderpath%\*build.bat) do ( 
  set "foldername=%%~nf" 
  move "%%f" "!foldername:~0,-5!" 
)

The wildcard *build.bat finds all batch files ending with "build" but wildcard "*.bat" shows them all.

Wasif
  • 14,755
  • 3
  • 14
  • 34
  • 1
    Wasif Hasan, is there a particular reason why you have a tendency to repeat other people's comments, as answers, both before confirmation by the OP, and, _in this case_, after both a comment and answer confirmation? It is clear on this occasion that your answer has not solved anything, I have. – Compo Dec 27 '19 at 17:39
  • No I started to answer it before the OP answered it. – Wasif Dec 27 '19 at 17:41
  • 1
    Unless it took at least 20 minutes to type, it is technically still a repeat of my earlier, comment, _(isn't it)?_ – Compo Dec 27 '19 at 17:44
  • Anyway, thank you. I will try to reduce these sorts of answers. The credit goes to you. – Wasif Dec 27 '19 at 17:46
  • @com: If you know the answer, why are you posting a comment? – IInspectable Dec 27 '19 at 21:19
  • Because @IInspectable, in my opinion the question was essentially an oversight by the OP, and not something which by answering would be of use to future searchers/questioners. In cases such as this, I do not feel the need to inflate my reputation/points tally unnecessarily. – Compo Dec 27 '19 at 21:49
  • @com: In that case you should have voted to close the question. – IInspectable Dec 27 '19 at 21:54
  • I thought I had, @IInspectable, now corrected as if a typo. – Compo Dec 27 '19 at 22:02