removing dates from filenames

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

removing dates from filenames

#1 Post by lalat06bag » 10 Jan 2018 15:35

Hi,

I have below set of files. How to remove the dates from the file name?
WTIN_PEG_FX_Wires-en-us_2017-12-20T024722674Z.pdf
WTIN_PEG_BX_2017-12-20T024722674Z.pdf
W_P_B_2017-12-20T024722674Z.pdf

Output should be
WTIN_PEG_FX_Wires-en-us.pdf
WTIN_PEG_BX.pdf
W_P_B.pdf
Last edited by lalat06bag on 10 Feb 2018 18:46, edited 3 times in total.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: removing dates from filenames

#2 Post by elzooilogico » 11 Jan 2018 03:27

if date is always 21 characters long (as in your example) smply remove the last 22 (date length + hyphen) characters from filename

Code: Select all

@echo off
setlocal enableDelayedExpansion
set /a dateLen=22
for /f %%v in ('dir /b') do (
  set "file=%%~nv" & rem fetch filename only
  set "file=!file:~0,-%dateLen%!%%~xv" & rem remove last n chars from filename and add extension
  echo !file!
)
endlocal
exit/B
This will not rename files, only echo the new file name. change

Code: Select all

echo !file!
to

Code: Select all

ren "%%v" "!file!"

lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

Re: removing dates from filenames

#3 Post by lalat06bag » 12 Jan 2018 12:16

Thank you. This worked perfectly fine.

lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

Re: removing dates from filenames

#4 Post by lalat06bag » 12 Jan 2018 14:04

Hi,

In response to above, I want to convert these pdfs to text. I have the pdftotext.exe in say xpdf folder which can convert by pdftotext abc.pdf abc.txt and it would convert it to txt.

How can I go to xpdf folder to use pdftotext.exe and convert the files in "alltextfiles" folder?

folder allpdffiles had all the pdfs
folder alltextfiles should be having all the text files after conversion.
the pdftotext exe is xpdf folder. so to execute pdftotext, I have to go to xpdf folder to execute this.

Thanks,

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: removing dates from filenames

#5 Post by elzooilogico » 13 Jan 2018 05:05

Simply add full paths to exe, source and destination files. give this a try (NOT TESTED)

Code: Select all

@echo off

:: %~dp0 is the directory this script is run from, so change
:: set "pdftotext=%~dp0pdftotext.exe" to whatever the actual location is (i.e)
:: set "pdftotext=C:\Program Files (x86)\xpdf\pdftotext.exe"
set "pdfToText=%~dp0pdftotext.exe"
:: do the same iwth source and destination folders
set "allPdfFiles=%~dp0allpdffiles"
set "allTxtFiles=%~dp0alltextfiles"
:: create the destination directory if it doesn't exist 
if not exist "%allTxtFiles%" mkdir "%allTxtFiles%"

for /f %%v in ('dir /b %allPdfFiles%\*.pdf') do (
  "%pdfToText%" "%allPdfFiles%\%%v" "%allTxtFiles%\%%~nv.txt" >NUL 2>&1 || echo Error converting %%v
)

EndLocal
exit/B

lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

Re: removing dates from filenames

#6 Post by lalat06bag » 13 Jan 2018 14:16

so super. Thank you a bunch. It worked so well. have a nice one.

lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

Re: removing dates from filenames

#7 Post by lalat06bag » 09 Feb 2018 12:25

In response to the above, need help in converting the files reading the content from a flat file. Please see.

Source folder would be having all the reports.
xpdf would be containing pdftotxt.exe which would convert pdf to text file.
Dest folder would be having the converted content.
daily_report.txt would contain the pdf files which need to be converted.

While source would be searched, it would pick all the files mentioned in the daily_report.txt with today's date, convert it and move it to the destination folder. So in the destination folder, there would not be the date part. It would be same in the flat file.txt.

The files would be looked upon for 2 hours in every 5 mins till all the files are found or the time is reached. If not found, it would be written to the error log.
Please note: there are few spaces in the file name.
Source:

XXXX_QRRPS Tran book - 0utgoing-en-in_2018-02-09T024757245Z.pdf
XXXX_QRRPS Tran book - Incoming-en-in_2018-02-09T024757245Z.pdf
XXXX_CB Vol By rce-en-in_2018-02-09T024757245Z.xlsx
XXXX_ABC_Rec_Eng-en-eu_2018-02-09T024757245Z.pdf
XXXX_ABC_Rec_Eng-en-eu_2018-02-09T024757245Z.xlsx
XXXX_QRRPS Tran book - Incoming-en-in_2018-02-08T024757245Z.pdf
XXXX_QRRPS Tran book - Incoming-en-in_2018-02-08T024757245Z.pdf
XXXX_CB Vol By rce-en-in_2018-02-08T024757245Z.xlsx
XXXX_ABC_Rec_Eng-en-eu_2018-02-08T024757245Z.pdf
XXXX_ABC_Rec_Eng-en-eu_2018-02-08T024757245Z.xlsx

daily_report.txt would be having
XXXX_QRRPS Tran book - 0utgoing-en-in.pdf
XXXX_ABC_Rec_Eng-en-eu.pdf

dest folder would be having
XXXX_QRRPS Tran book - 0utgoing-en-in.txt
XXXX_ABC_Rec_Eng-en-eu.txt

lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

removing dates from filenames

#8 Post by lalat06bag » 10 Feb 2018 16:09

Code: Select all

@echo off &setlocal

setlocal enableDelayedExpansion
set /a dateLen=22

set "pdfToText=C:\Users\u595142\Desktop\xpdf32\pdftotext.exe" & rem source for the pdftotext exe
set "src=C:\Users\U595142\Desktop\allPdfFiles"
set "dest=C:\Users\U595142\Desktop\Today_Files"
set "allTxtFiles=C:\Users\u595142\Desktop\allTxtFiles" & rem source txt folder
set "list=C:\Users\U595142\Desktop\catalog\daily_report.txt"
set "finalTxt=C:\Users\U595142\Desktop\final"
IF not EXIST "C:\Users\U595142\Desktop\catalog\error.log" type nul>"C:\Users\U595142\Desktop\catalog\error.log"
IF not EXIST "C:\Users\U595142\Desktop\Today_Files" mkdir "C:\Users\U595142\Desktop\Today_Files"
IF not EXIST "C:\Users\U595142\Desktop\allTxtFiles" mkdir "C:\Users\U595142\Desktop\allTxtFiles"

for /f "delims=." %%i in ('wmic os get LocalDateTime /value') do for /f %%j in ("%%i") do set "%%j"
set "today=_%LocalDateTime:~,4%-%LocalDateTime:~4,2%-%LocalDateTime:~6,2%T*"

set "n=0"
:loop
set /a "n+=1"
set "flag="
>"C:\Users\U595142\Desktop\catalog\error.log" (for /f "usebackq delims=" %%i in ("%list%") do if exist "%src%\%%~ni%today%%%~xi" (copy "%src%\%%~ni%today%%%~xi" 

"%dest%\") else (set "flag=1" &echo "%%~ni%today%%%~xi" not found))

if defined flag if %n% lss 2 (timeout /t 2 /nobreak &goto loop)


for /f "delims=" %%v in ('dir /b %dest%\*.pdf') do (
  "%pdfToText%" "%dest%\%%v" "%allTxtFiles%\%%~nv.txt" >NUL 2>&1 || echo Error converting %%v
)


@RD /S /Q "C:\Users\U595142\Desktop\Today_Files"


for /f "delims=" %%v in ('dir /b /s %allTxtFiles%\*.txt') do (
  set "file=%%~nv" & rem fetch filename only
  set "file=!file:~0,-%dateLen%!%%~xv" & rem remove last n chars from filename and add extension
ren "%%v" "!file!" 
)



for /f "delims=" %%v in ('dir /b /s %allTxtFiles%\*.txt') do (
copy "%allTxtFiles%\*.*" "%finalTxt%\"
)

@RD /S /Q "C:\Users\U595142\Desktop\allTxtFiles"

EndLocal
exit/B

This is the code for the above requirement.
Last edited by lalat06bag on 10 Feb 2018 17:31, edited 3 times in total.

lalat06bag
Posts: 51
Joined: 10 Jan 2018 15:21

Re: Help Me- removing dates from filenames

#9 Post by lalat06bag » 10 Feb 2018 16:55

Another few queries on this code.

Here while converting the pdf to txt, i am keeping the required pdfs in a separate folder temporarily "Today_Files" , converting the text to AllTextFiles and finally moving all the text to final folder.
In the final folder, there would be the same files with different dates as well. Need expert opinion here, if we can eliminate creating the above 2 folders and move directly the converted files to the final folder from allPdfFiles?

Post Reply