简体   繁体   中英

Batch Script To Copy Filenames?

I'm trying to create a batch script that:

  • Copies the new files' filenames
  • Pastes each filename in a new line in a text file before the last line

For example: I have files named Picture.JPG and Picture2.JPG in the folder. The batch needs to copy the filenames "Picture" and "Picture2" and pastes it in textfile.txt, which already has a last line that I don't want to overwrite, so it would appear like this:

Picture
Picture2
This is the last line

Note that I don't want the.JPG extension copied.

Any ideas?

This should work, you'll need to put it in a cmd.file

for %%a in (*.jpg) do echo %%~na >> Tem.txt
type textfile.txt >> tem.txt
copy tem.txt textfile.txt
del tem.txt

Read this question to extract the filename, as input get the output of the ls or dir command in a pipe and then append it to your textfiloe.txt using the ">>" operator.

To append to the start of the file check this

This script accepts two parameters:

  • %1 – the name of the text file;

  • %2 - the working directory (where the *.jpg files are stored).

@ECHO OFF

:: set working names
SET "fname=%~1"
SET "dname=%~2"

:: get the text file's line count
SET cnt=0
FOR /F "usebackq" %%C IN ("%fname%") DO SET /A cnt+=1

:: split the text file, storing the last line separately from the other lines
IF EXIST "%fname%.tmp" DEL "%fname%.tmp"
(FOR /L %%L IN (1,1,%cnt%) DO (
  SET /P line=
  IF %%L==%cnt% (
    CALL ECHO %%line%%>"%fname%.tmplast"
  ) ELSE (
    CALL ECHO %%line%%>>"%fname%.tmp"
  )
)) <"%fname%"

:: append file names to 'the other lines'
FOR %%F IN ("%dname%\*.jpg") DO ECHO %%~nF>>"%fname%.tmp"

:: concatenate the two parts under the original name
COPY /B /Y "%fname%.tmp" + "%fname%.tmplast" "%fname%"

:: remove the temporary files
DEL "%fname%.tmp*"

The get the text file's line count part simply iterates through all the lines, while increasing the counter. You can use a different approach if you know for sure what the last line is like, or if you know that it must contain a certain substring (even if it's just one character). In that case you can replace the FOR loop used above with this FOR loop:

FOR /F "delims=[] tokens=1" %%C IN ('FIND /N "search term" ^<"%fname%"') DO SET cnt=%%C

where search term is the term that can be matched by the last line.

Paste the below in a bat file in the folder of jpegs with a textfile called mylistofjpegfiles.txt:

::Build new list of files
del newlistandtail.txt 2>nul
for /f %%A in ('dir *jpg /b') Do (echo %%~nA >> newlistandtail.txt)


:: Add last line to this new list
tail -1 mylistofjpegfiles.txt >> newlistandtail.txt


:: Build current list of files without last line
del listnotail.txt 2>nul
for /f %%A in ('tail -1 mylistofjpegfiles.txt') Do (findstr /l /V "%%A" mylistofjpegfiles.txt >> listnotail.txt)

:: Compare old list with new list and add unmatched ie new entries
findstr /i /l /V /g:mylistofjpegfiles.txt newlistandtail.txt >> listnotail.txt  

:: add last line
tail -1 mylistofjpegfiles.txt >> listnotail.txt

:: update to current list
type listnotail.txt > mylistofjpegfiles.txt

:: cleanup
del newlistandtail.txt 
del listnotail.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM