0

I am hoping to get some help with this, I am thinking of using Command Line to do the following:

  • Move ONE file in a folder (that has hundreds of files) to a new folder (new folder is the folder this folder is IN, so up one directory)

  • Rename the file (basically all files have 852 in the file name and I want to delete anything between 852 and .TXT)

Your help is really appreciated!!

Thanks!!

  • 3
    What's your question? Are you asking for someone to write the command for you? Do you know how the copy command works? Have you tried writing this command and had problems with it? – Todd Wilcox Feb 21 at 17:22
  • How do you want to choose one file from among those hundreds? – JosefZ Feb 22 at 20:55
  • Hi, yes I was looking for some help. I’m familiar with the move command but not sure how to get it to move just ONE file (by just selecting any file, not by title of file) and renaming it – ninjamary Feb 23 at 23:06
  • I didn’t know if there was a way to just select One file, any file, not by the name – ninjamary Feb 23 at 23:06
0

The following commented batch script 955137.bat could do the job - if I understand your (slightly vaguely formulated) question:

@ECHO OFF
SETLOCAL EnableExtensions
:: production
:: store folder (that has hundreds of files) name to a variable
    ::       ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓  change to match your conditions
set "_folder=D:\test\955137\sub955137"
:: Change the current directory/folder and store the previous folder/path
pushd "%_folder%"
:: get (any) one file, store its name to the _fileOld variable 
for %%G in (*.txt) do set "_fileOld=%%G"
::  
   :: To delete everything after the string '852'  
   :: first delete '852' and everything before it
   SET "_fileAux=%_fileOld:*852=%"

   ::Now remove this from the original string
   CALL SET "_fileNew=%%_fileOld:%_fileAux%=%%.TXT"
::
   :: debug: show renaming rule
   echo %_fileOld% - %_fileAux% = %_fileNew%  
:: silently copy file up one directory (renaming the target) 
>NUL copy /B "%_fileOld%" ..\"%_fileNew%"
:: debug: show copied file
tree /F %CD%\.. | findstr /I /V "volume"

:: Change directory back to the path most recently stored by the PUSHD command.
popd

Output shows the renaming rule for chosen file (actually, above code always sets the _fileOld variable to the last file in their alphabetical order using the for %%G … loop).

d:\bat> d:\bat\SF\955137.bat
ONExy852ONEab.txt - ONEab.txt = ONExy852.TXT
D:\TEST\955137
│   ONExy852.TXT
│
└───sub955137
        axy852aab.txt
        filexy852fileab.txt
        folderxy852folderab.txt
        inxy852inab.txt
        Movexy852Moveab.txt
        ONExy852ONEab.txt

Please note that above output comes from a testing environment created ad-hoc as follows:

@ECHO OFF
SETLOCAL EnableExtensions
:: build testing environment - start
::   store folder (that has hundreds of files) name to a variable
set "_folder=D:\test\955137\sub955137"
::   create a folder
2>NUL MD "%_folder%"
pushd "%_folder%"
::   create some files there in %_folder%
for %%G in (Move ONE file in a folder) do >"%_folder%\%%Gxy852%%Gab.txt" echo %%G
::   remove all files from previous testing
2>NUL erase %CD%\..\*852*.txt
::   built testing environment: list files
tree /F %CD%\.. | findstr /I /V "volume"
::   build testing environment - done
popd
ENDLOCAL

with the following output:

d:\bat> d:\bat\SF\955137create.bat
D:\TEST\955137
└───sub955137
        axy852aab.txt
        filexy852fileab.txt
        folderxy852folderab.txt
        inxy852inab.txt
        Movexy852Moveab.txt
        ONExy852ONEab.txt

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.