Windows CMD: add suffix with spaces to all subfolders - cmd

This question kind of look like this one here
Windows-cmd-add-suffix-to-all-files-in-folder, but it has some additional elements I'm not able to overcome.
I have a main folder, containing many subfolders, with a lot of .xlsb files, and I wish to add a postfix " 2018" to eaxh file, e.g. after renaming a file with name "abc.xlsb" will be called "abc 2018.xlsb". I'm having problem with finding a code, making a script in the command line that will do this for all subfolders.
For now, this is what I have for one folder:
rename *.xlsb "* 2018.xlsb"
,but the resulting name for, e.g., "a.xlsb" comes out "a.xlsb 2018.xlsb".
Also, how do I do this for all folders?
EDIT: I found a command to rename for all subfolders:
for /r %x in (*.xlsb) do ren "%x" *.xlsb
,but
for /r %x in (*.xlsb) do ren "%x" "* 2018.xlsb"
gives, e.g. for a file "abc.xlsb" --> "abc.xlsb 2018.xlsb"

This is pretty straightforward in PowerShell. Use the BaseName and Extension members separately. When you are confident that the files will be renamed correctly, remove the -WhatIf from the Rename-Item cmdlet.
Get-ChildItem -File -Recurse -Filter '*.xlsd' |
ForEach-Object {
$newname = $_.BaseName + ' 2018' + $_.Extension
Rename-Item -Path $_.FullName -NewName $newname -WhatIf
}
If you must run this from a cmd.exe shell, then put the PowerShell code above into a filename such as renappend.ps1. Then run it as shown.
powershell -NoLogo -NoProfile -File renappend.ps1

Related

How can I use a bat file to mass-copy files from child-dir to root?

How can I use windows bat or powershell to mass-copy files from child-dirs to the root/main directory? eg,
c:\dir\1
c:\dir\2
c:\dir\3
etc
to
c:\dir
without having to copy-paste them all manually.
In a batch file:
for /d %%d in ("C:\Dir\*") do for %%f in ("%%d\*") do copy "%%f" "%%d\.."
Interactively:
for /d %d in ("C:\Dir\*") do for %f in ("%d\*") do copy "%f" "%d\.."
Hint: for /d iterates over directories. Use for /? for more details.
If it's only the files you want to move then a quick a dirty method would be something like:
$InputFolder = "C:\Dir"
$FilesToMove = Get-ChildItem -LiteralPath $InputFolder -Recurse | where { ! $_.PSIsContainer }
foreach ($File in $FilesToMove)
{
Move-Item -LiteralPath $File.PSPath -Destination $InputFolder -Force
}
So if for example you have:
C:\Dir\MyDir1\File.txt it will move it to c:\dir\file.txt
However, no folders will be moved.
eg say c:\Dir\MyDir2\AnotherDir\ contains File1.txt and Fle2.txt only the .txt files will get moved, c:\Dir\MyDir2\AnotherDir\ would remain intact but without any files.

Listing files and folders and outputting it as .csv

I'm trying to write a batch script to list all the folders, sub-folders and files inside a directory, and then output everything to a .csv-file. I have tried this using the tree command but I also need the "Creation date" and "Last modified" date to be included. The tree command doesn't seem to support that. Is there any other way to do it?
Example:
tree "C:\Windows" /A /F > "C:\Log.csv"
Use a PowerShell one liner:
Get-ChildItem -Recurse | Select-Object FullName,CreationTime,LastWriteTime|Export-Csv C:\log.csv -NotypeInformation
if necessary wrapped in a batch:
powershell -NoP -C "Get-ChildItem -Recurse | Select-Object FullName,CreationTime,LastWriteTime|Export-Csv C:\log.csv -NotypeInformation"
What about Dir /S *.*?
The /S stands for "go through the directory and all subdirectories".
Sorry. I missed the part where you needed the creation dates. Those ones can be achieved as mentioned in this post.

Rename files having no extension

How can we rename all files in a folder having no extension at all to ".something".
I have tried ren *.* *.jpeg and few more but nothing working
* matches any extension. You want to match no extension, so don't supply one: ren *. *.jpeg.
The above only works in cmd -- PowerShell's wildcards work differently, and mostly don't do anything special with extensions. What's more, batch renaming in PowerShell works differently. So:
dir -Filter "*." -File | ren -NewName { $_.name -replace "$", ".jpeg" }
"rename *. *.jpg" weirdly will bring up "The syntax of the command is incorrect." if there are no files without extensions in the location. I thought the command stopped working at first! So just letting people know if they have the same thing come up.
This is tested and works for me:
ls -file | ?{$_.extension -eq ''} | %{ren $_ ($_.name + '.jpeg')}
A slight variation:
dir -filter *. -file | rename-item -NewName {"$($_.name)`.jpg"}
You did everything correct but instead of *.* you ought to use *. as *.* searches for all files with all extensions but the former searches for all files with no extension and that is what you want. So here is your code:
rename *. *.something
You can refer to this answer for further help.
try this:
Get-ChildItem "c:\temp" -file "*." | rename-item -NewName {"{0}.something" -f $_.fullname}
For me as simple as ren * *.jpeg worked.

cmd Search for files from list of partial filenames then copy to folder

I have a text file list of approx 120,000 filenames. Many of the files on the list are in a folder or it's subfolders, but with slight variations on the filenames.
so I want to search using the list of partial filenames and copy the matches to another folder.
Each line on the list is a name and a title separated by a bar for example:
A Name|The Title
John Smith|A Life
The files are various text formats and all have extra stuff in the filenames like:
A Name - The Title V1.4 (html).lit
John Smith - A Life: Living on the Edge [MD] (pdf).rar
I've tried the code from this thread
and this thread but neither are finding any of the files. Can anyone help please.
This PowerShell script assumes that if both the first field "name" and the second field "title" are anywhere in the filename that it should be copied. When you are confident that the correct files will be copied, remove the -WhatIf from the Copy-Item command.
Note that this does not address the issue of multiple files with the same name.
If you wanted to require the "name" field to be at the beginning of the string, you could add it to the match expression. $_.Name -match '^'+$pair.name. If you want the matches to be case sensitive, use -cmatch.
$sourcepath = 'C:\src'
$targetpath = 'C:\other'
$searchpairs = Import-Csv -Header "name","title" -Delimiter "|" -Encoding ASCII -path .\mdb.txt
foreach ($pair in $searchpairs) {
Get-ChildItem -Recurse -File -Path $sourcepath |
Where-Object { ($_.Name -match $pair.name) -and ($_.Name -match $pair.title) } |
ForEach-Object { Copy-Item $_.FullName $targetpath -WhatIf}
}
Adjust the paths and you should be good with this one:
#ECHO OFF
REM **************************************************
REM Adjust location of list
SET list=C:\adjust\path\list.txt
REM Source dir
SET source=C:\adjust\path\source
REM Target dir
SET destination=C:\adjust\path\destination
REM **************************************************
FOR /F "tokens=1,* delims=|" %%A IN (%list%) DO (
ECHO.
ECHO %%A - %%B
CALL :copy "%%A - %%B"
)
ECHO.
ECHO Done^!
PAUSE
EXIT
:copy
FOR /R "%source%" %%F IN (*) DO (
ECHO "%%~nF" | FINDSTR /C:%1 >nul && COPY "%%~fF" "%destination%\%%~nxF" && EXIT /B
)
EXIT /B
Be aware: this requires the scheme in list.txt to be A|B and the scheme of every file to be copied *A - B* (including spaces) while * may be no or any character(s).
Might not be the solution you are looking for, but I ditched batch scripting years ago. I use Powershell instead, and simply call the Powershell script from batch file.
Here is the code in case you are interested,
searchfiles.ps1
$searchStrings = #("city", "sniper") # Declare an array to iterate over.
foreach ($string in $searchStrings) {
Get-ChildItem -Path D:\Movies\Movies | ? { $_ -match $string }
}
And now call the Powershell script from batch file.
searchfiles.bat
Powershell.exe -ExecutionPolicy RemoteSigned -Command "& searchfiles.ps1 -Verb RunAs"
Hope it helps!
That's not to say I don't use batch scripting at all. I will use them only for simpler operations, like calling another script, or opening a folder, etc. With Powershell, I love taking the help of the underlying .NET framework and sweet piping!

How to add word to beginning of multiple files using powershell

Today I walked into my office and a couple guys were renaming hundreds and hundreds of files manually on the computer. I looked up ways to rename multiple files. I currently know how to replace a value with another value. Say for example Dir | Rename-Item –NewName { $_.name –replace " ","_" } which replaces all spaces in a file name to an underscore.
However, for my particular task, I need to add just a word in front of all of the file names, for example:
Johnhancock.pdf, AnitaMann.pdf, AmandaHugginkiss.pdf
into
(scores)Johnhancock.pdf, (scores)AnitaMann.pdf, (scores)AmandaHugginkiss.pdf
How would I would I do this? The parentheses are needed for this particular project.
In that scriptblock you just have to generate the name that you want for each item. $_ represents the current item. For your example it would be:
Get-ChildItem | Rename-Item -NewName { "(scores)$($_.Name)" }
This will process an entire directory tree with a batch file. Remove the echo if what you see on the screen matches what you want. Remove the /r to process only the current folder.
#echo off
for /r %%a in (*.txt) do echo ren "%%a" "(scores)%%~nxa"
What worked for me is the code as follows (I could not get the above to work):
Get-ChildItem *.pdf | rename-item -NewName { "(score)" + $_.Name }

Resources