Experimenter
Experimenter

Reputation: 167

Add prefix to each file name in cmd and list them

Below command adds prefix to all files and lists them in out.txt. But

  1. it also lists the folder names
  2. and adds file path which I don't want.

Is it possible the desired output in cmd itself? Any help will be appreciated.

Command:

    for /f %f in ('dir /b /s *.*') do echo dt obsolete %f >> out.txt

Current output:

dt obsolete D:\workdir\src1\drafttest2\python 
dt obsolete D:\workdir\src1\drafttest2\draftingdimension\ind 
dt obsolete D:\workdir\src1\drafttest2\draftingdimension\ind\GB_005.seq 
dt obsolete D:\workdir\src1\drafttest2\draftingdimension\ind\GB_005_py.py 

Desired output:

dt obsolete GB_005.seq 
dt obsolete GB_005_py.py

Upvotes: 0

Views: 1174

Answers (1)

user7818749
user7818749

Reputation:

You can have a go at this from cmdline:

for /f %f in ('dir /b /s *.*') do echo dt "obsolete_%~nxf">>out.txt

or if you want to include the path:

for /f %f in ('dir /b /s *.*') do echo dt "%~dpfobsolete_%~nxf">>out.txt

Not sure if you plan to do this for files only or files and folders, but if files only, then:

for /f %f in ('dir /b /s /a-d *.*') do echo dt "%~dpfobsolete_%~nxf">>out.txt

To run the above from batch file, you need to double up on all the %

As mentioned in my comment as well, from cmd.exe run for /? to see all the help you need on variable expansion.

Upvotes: 1

Related Questions