0

I have files with the following name

  • GB_20141001
  • JPN_20141001
  • GB_20141002
  • TH_20141003

I want the date at the end of file name to prepended to its content. The content of each file may look like this

ABD10224
CHG20991
GHJ02933

and would become this:

20141001
ABD10224
CHG20991
GHJ02933

is it possible to create a batch file to do that

2
  • This is more of a SO question and for that to be a great question you need to at least so some effort have you tried anything already? – Matt Nov 4 '14 at 5:17
  • I tried for single file but couldn't get the date part. – Muhaimin Nov 4 '14 at 5:19
1

Note - None of this code is tested, so there may be some bugs. But the concepts are all correct

If none of your files have more than one _, then:

@echo off
for /f "eol=_ delims=_ tokens=1,2" %%A in (
  'dir /b /a-d *_????????^|findstr /rx "[^._][^._]*_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"'
) do ((
  echo %%B
  type "%%A_%%B"
) >"%%A_%%B.new" & move /y "%%A_%%B.new" "%%A_%%B" >nul )

If there may be more than one _, then:

@echo off
setlocal
for /f "eol=_ delims=_ tokens=1,2" %%F in (
  'dir /b /a-d *_????????^|findstr /rx "[^.][^.]*_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"'
) do call :processFile "%%F"
exit /b

:processFile
set "file=%~1"
for %%A in ("%file:_=" "%") do set "dt=%%A"
>"%~1.new" (
  echo %dt%
  type %1
)
move /y "%~1.new" %1 >nul
exit /b

Or, if you get a copy of repl.bat, it simplifies to:

@echo off
for /f "eol=: delims=: tokens=1,2" %%A in (
  'dir /b /a-d *_????????^|repl "^[^.]+_(\d{8})$" "$&:$1" a'
) do ((
  echo %%B
  type "%%A"
) >"%%A.new" & move /y "%%A.new" "%%A" >nul )
0

If you have Cygwin installed or otherwise have access to a bash shell, then try this:

for f in *; do sed -i "1 s/^/${f##*_}\n/" "$f"; done

How it works

  • for f in *; do

    This starts a loop over all files in the current directory.

  • sed -i "1 s/^/${f##*_}\n/" "$f"

    sed is the tool for editing files. -i tells sed to edit the file in-place. 1 tells the sed to apply this change only to the first line. s/^/${f##*_}\n/ is a substitute command. It normally looks something like s/old/new/. In our case, old is ^ which is a regular expression that matches at the beginning of a line. The beginning of the line is replaced with ${f##*_}\n/ where ${f##*_} is the file name with everything up through the last underline character removed and \n is new line character. This places the date on a new line at the beginning of the file

  • done

    This marks the end of the for loop.

Example

Let us start with a directory with the sample file:

$ ls
GB_20141001
$ cat GB_20141001 
ABD10224
CHG20991
GHJ02933

We apply our command:

$ for f in *; do sed -i "1 s/^/${f##*_}\n/" "$f"; done

The file now has the date as the first line:

$ cat GB_20141001 
20141001
ABD10224
CHG20991
GHJ02933
4
  • 1
    Note the Windows tag. This would require some unix tool ports such as cygwin. – Paul Nov 4 '14 at 5:33
  • @Paul Oops. Thanks. Answer now updated with that qualifier. – John1024 Nov 4 '14 at 5:36
  • is there anything with pure windows – Muhaimin Nov 4 '14 at 6:30
  • @MuhaiminAbdul Most likely, but I am not the right person to provide that answer. – John1024 Nov 4 '14 at 6:36

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.