get file name of directory and subdirectory using CMD - cmd

I am making an inventory for which I need to copy all file names in an excel sheet. Right now I am using cmd command "dir /s /w /p > file.txt" for every folder. Problem is that when I get the txt files, file names are in same line, and I have to select them and copy paste in Excel sheet. I need a command that gives me all files names should come in new line so that I can easily copy all file names at once and paste them.

You are getting file names in the same line because you use the /w (wide) option, which does exactly this, check /w parameter here https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/dir
To solve your problem simple don't use the /w parameter. You can see the result below

Related

Changing file name in cmd using "ren"

I am trying to change the file name of "summary-SUCCESS.txt" or "summary-FAILED.txt" to add text to the front of the file name such as:
"Name1-summary-SUCCESS.txt"
"Name2-summary-SUCCESS.txt"
"Name3-summary-SUCCESS.txt"
... and so on
I am currently using the cmd
ren summary-* name1-summary*
and it takes away characters from the original file name, an example of what is output below:
"Name1-summary-S.txt"
"Nam1-summary-SS.txt"
"Na1-summary-ESS.txt"
"N1-summary-CESS.txt"
"N-summary-CCESS.txt"
"-summary-UCCESS.txt"
How would I go about adding a certain name to the beginning of a file name without losing any of the other characters in the original file name?
You need to use a for loop to iterate through all the matching files.
In a command prompt window (cmd) it looks like this:
for %I in ("summary-*.txt") do ren "%~I" "Name1-%~nxI"
In a batch script (batch-file) it looks like this:
for %%I in ("summary-*.txt") do ren "%%~I" "Name1-%%~nxI"
You may be interested in this thread on SuperĀ User:
How does the Windows RENAME command interpret wildcards?

Script to change the folder name for 10,000 folders in windows

I want to change the folder name for 10,000 folders in windows.
I have old structure available and I want to change it to new one. For example:
Old structure: C:\Documents to read\Important\Domain\Latest\Today
New structure: C:\Documents\Important\Domain\new\yesterday
Is there any way through which I can change the 10,000 flies by providing folder structure in a batch file?
(old folder structure to new folder structure without impacting the underneath files)
I tried - ren "old file name" "new file name" in command prompt but it is very specific to particular folder.
Please help. I am very new to the command prompt. Any kind of help is very much appreciated.
Thank you!
To rename a folder you have to use mv /home/user/oldname /home/user/newname
after this you have to use a for loop for all your folder
Windows has limited batch file capability, so what I've done is:
DIR /S /AD /B > files.tsv to output all the files in bare full format
import files.tsv into Excel
new column with a formula to generate "after"
new column with a formula to create a REN command, e.g. ="REN """ & A1 & """ """ & B1 & """"
paste that column into a batch file DOIT.BAT
very important - backup all the files! very easy to mess up
run DOIT.BAT
DIR /S /AD /B does recurse into all subdirectories, which is annoying, but it does list them in hierarchical order, so it might be easy to edit out the ones you don't want.
The extra quotes are to make sure spaces in directories and file names for REN are handled properly.

Windows CMD search

Description:
Since my previous post of using a .bat file to search for file names (Find multiple files from the command line),
I have figured out how to run a .bat file to search a server for specific file names. Now, I want to do the same thing but I want to include in my search the entire contents of a directory.
All of this will be done in windows command prompt and of course a notepad file if needed. I do not have access to linux or unix so please no responses that include such.
Example -
I Drive
+Drawings
++Structural
So for the example above I want to take the entire contents of dir structural (which may be 1000s of .dwg) and using a .bat file search a server with it.
Also I put these commands in notepad and renamed it from a .txt to a .bat
My single file search
dir [file.dwg] /s/4
Entire Directory Search (which does not work this is what I am trying to do)
dir [original dir] /s/4
After i finished writing my .bat file in notepad I would simply put it in the server directory location that I needed to search and run it.
I hope I have made myself clear and I hope that you can help because Im not sure what to do here.
See(Folder Comparisons Via Command Line) for what I am trying to do only I need to compare the directory and all sub directories.
In an effort to understand what you want, here is an example batch file:
DIR G:\Structural /S /B
G:\Structural\cad2012.dwg
G:\Structural\cad2013.dwg
G:\Structural\cad2014.dwg
G:\Structural\photo2012.jpg
If you just want to find the files with 2012
DIR G:\Structural /S /B | FINDSTR 2012
G:\Structural\cad2012.dwg
G:\Structural\photo2012.jpg
If you just want to find the dwg files from 2012
DIR G:\Structural /S /B | FINDSTR 2012 | FINDSTR /R /I dwg$
G:\Structural\cad2012.dwg
Have a look at what I wrote in response to the question "batch file - Compare in windows command prompt" which I think is getting close to what you want to do.

How to use a batch file to delete a line of text in a bunch of text files?

I have a bunch of txt files in my D drive which are placed randomly in different locations. Some files also contain symbols. I want a batch file so that I can delete their specific lines completely at the same time without doing it one by one for each file and please refer to a code which does not create a new text file at some other location with the changes being incorporated i.e. I do not want the input.txt and output.txt thing. I just need the original files to be replaced with the changes as soon as I click the batch file.
e.g
D:\abc\1.txt
D:\xyz\2.txt etc
I want both of their 3rd lines erased completely with a single click and the new file must be saved with the same name in the same location i.e. the new changed text files must replace the old text files with their respective lines removed. Maybe some sort of *.txt thing i.e i should be able to change all the files with the .txt extensions in a drive via a single batch file perhaps in another drive,not placing my batch file into each and every folder separately and then running them. Alternatively a vbs file is also welcomed.
This uses a helper batch file called findrepl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697
Place findrepl.bat in the same folder as the batch file below.
It will search for every *.txt file on drive d: and remove line 3.
#echo off
for /r "d:\" %%a in (*.txt) do (
echo processing "%%a"
type "%%a"|findrepl /v /o:3:3 >"%%a.tmp"
move "%%a.tmp" "%%a" >nul
)
pause

Win 7: CMD batch file for creating directories based on filenames

I'm working on a CMD line batch file in a Win7 environment that will create directories based upon the filenames listed in a directory.
I am using this code, but the output created is partial and incomplete with only
setlocal enabledelayedexpansion
for /r %%i in (*.wav) do (
set filename1=%%i
set folder1=!filename1:~4,10!
mkdir !folder1!
)
pause
I have this script saved as a CMD file in text format in the source directory, on a local harddrive, though it is in a subdirectory.
The directory output is partial and broken, with garbled output and the numbers of directories created does not match the number of files, and the created directories seem to nest. I've researched this and can't seem to find a definitive answer.
It's not entirely clear what it is you are trying to accomplish. Are you trying to create a directory within the same directory containing the wav file, just without the .wav extension? If so, you're missing some quotation marks and you're stripping the wrong end of the filename off. If that's what you are trying to accomplish, it can actually be done with a single command, no batch script needed. Type this at the command prompt:
for /r %I in (*.wav) do mkdir "%~pnI"
Of course, if you still want it in a batch script, use %%I and %%~pnI with double percents instead of single. See the last couple of pages of help for for an explanation of how %%~pnI works.

Resources