1

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?

Andriy M
  • 76,112
  • 17
  • 94
  • 154
bei
  • 933
  • 3
  • 9
  • 17

4 Answers4

5

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
Andy Morris
  • 3,393
  • 1
  • 21
  • 20
  • That is a great start! What if I want to echo the filename in between two lines? Right now it would just add to the beginning of the file, right? Say I want to insert the filename in between the line "First Line" and the line "Last line", how would it work? – bei May 27 '11 at 17:26
1

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

Community
  • 1
  • 1
  • What if I need to append it in between two lines? First line Picture Picture2 Last line The idea is with each newly added line, it should go right before the last line but after the last picture name. – bei May 26 '11 at 19:30
  • And I'm looking for a batch script solution, not bash. Thanks for trying though! – bei May 26 '11 at 23:05
1

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.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
  • I do know for sure what the last line is like, it's "" without the quotation marks. And I'm using the FOR loop with the "search term" as "". But for some reason, the batch deletes the specified search term... – bei May 27 '11 at 21:23
0

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
jack
  • 137
  • 2
  • tail is inlcuded in resource kit or bat code is here: http://ss64.org/viewtopic.php?id=506 – jack May 27 '11 at 13:24