How to add word to beginning of multiple files using powershell - windows

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 }

Related

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.

Windows CMD: add suffix with spaces to all subfolders

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

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!

Batch or Bash to remove a timestamp in thousands of files [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I made a major mistake today in handling my selfhosted backup system and wound up deleting about half of my user folder. Luckily, the backup existed and was able to copy all of my data over. The bad part is each file that was restored has a timestamp in the filename.
So a file that was filename.file is now filename~20170401-1999.file
I'm not too bright when it comes to batch files or what not, but is there any way a BAT could be programmed to run through every single filename and take out the timestamp? Every single timestamp starts with the ~ and ends with the period of the filetype. So removing everything including and after the tilde and stopping at the character before the period would fix the problem.
There are some 4600 files needing fixed with a wide variety of filetype extensions. I am on Windows 10 Pro and able to user powershell or bash to fix the error. I have the Windows Linux Subsystem installed, so a bash script is also acceptable.
PowerShell offers a simple solution:
Get-ChildItem -Recurse -File -Filter *~* |
Rename-Item -NewName { $_.Name -replace '~[^.]+' } -WhatIf
-WhatIf previews the renaming operations; remove it, once you've confirmed that the command will work as intended.
For simplicity I've made the assumption that any filename containing ~ must be renamed; the matching can be refined, if false positives must be eliminated:
Get-ChildItem -Recurse -File *~[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9].*
Matching is limited to files (-File), located anywhere in the current directory's subtree (-Recurse).
To only match files located directly in the current directory, remove -Recurse.
The script block ({ ... }) passed to the Rename-Item cmdlet's -NewName parameter calculates the new filename:
$_.Name returns the input file's name ($_ is PowerShell's automatic variable for representing the input object at hand).
-replace '~[^.]+' performs a regex-based string replacement: '~[^.]+' matches a ~ followed by any nonempty sequence (+) of characters other than . ([^.]).; not specifying a replacement string implicitly uses the empty string, so that the matching part is effectively removed from the name.
Just some idea from the top of my head.
You can iterate over the files, and for each filename, you can replace the sub-string that match the regex /~\d+-\d+/ with "" (i.e., empty string) and use the modified string as the file's new name.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /s /a-d "%sourcedir%\*" '
) DO (
FOR /f "tokens=1,2delims=~" %%p IN ("%%~na") DO (
IF "%%q" neq "" ECHO(REN "%%a" "%%~p%%~xa"
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.
Perform a directory scan with subdirectories in basic form withot directornames and assign to %%a. Then tokenise the name part of the full name found into %%p and %%q using ~ as a delimiter. If the second token is not empty then we have a timestamp in %%q so form the new filename from the part in %%p and the extension from %%a
variation of mklement0 without regex
Get-ChildItem -Recurse -File -Filter *~*.file |
rename-item -NewName {"{0}{1}" -f ($_.BaseName -split "~")[0], $_.Extension } -WhatIf
a short version:
gci -Rec -File -Filter *~*.file | ren -n {"{0}{1}" -f ($_.Base -split "~")[0], $_.Extension } -WhatIf

Replace or delete certain characters from filenames of all files in a folder [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
How do I delete certain characters or replace certain characters with other characters by some batch file execution, for filenames of all files in a Windows folder in one go, is there a DOS command for that?
Use PowerShell to do anything smarter for a DOS prompt. Here, I've shown how to batch rename all the files and directories in the current directory that contain spaces by replacing them with _ underscores.
Dir |
Rename-Item -NewName { $_.Name -replace " ","_" }
EDIT :
Optionally, the Where-Object command can be used to filter out ineligible objects for the successive cmdlet (command-let). The following are some examples to illustrate the flexibility it can afford you:
To skip any document files
Dir |
Where-Object { $_.Name -notmatch "\.(doc|xls|ppt)x?$" } |
Rename-Item -NewName { $_.Name -replace " ","_" }
To process only directories (pre-3.0 version)
Dir |
Where-Object { $_.Mode -match "^d" } |
Rename-Item -NewName { $_.Name -replace " ","_" }
PowerShell v3.0 introduced new Dir flags. You can also use Dir -Directory there.
To skip any files already containing an underscore (or some other character)
Dir |
Where-Object { -not $_.Name.Contains("_") } |
Rename-Item -NewName { $_.Name -replace " ","_" }
A one-liner command in Windows PowerShell to delete or rename certain characters will be as below. (here the whitespace is being replaced with underscore)
Dir | Rename-Item –NewName { $_.name –replace " ","_" }
The PowerShell answers are good, but the Rename-Item command doesn't work in the same target directory unless ALL of your files have the unwanted character in them (fails if it finds duplicates).
If you're like me and had a mix of good names and bad names, try this script instead (will replace spaces with an underscore):
Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace(" ", "_") }
This batch file can help, but it has some limitations. The filename characters = and % cannot be replaced (going from memory here) and an ^ in the filenames might be a problem too.
In this portion %newname: =_% on every line in the lower block it replaces the character after : with the character after = so as it stands the bunch of characters are going to be replaced with an underscore.
Remove the echo to activate the ren command as it will merely print the commands to the console window until you do.
It will only process the current folder, unless you add /s to the DIR command portion and then it will process all folders under the current one too.
To delete a certain character, remove the character from after the = sign. In %newname:z=% an entry like this would remove all z characters (case insensitive).
#echo off
for /f "delims=" %%a in ('dir /a:-d /o:n /b') do call :next "%%a"
pause
GOTO:EOF
:next
set "newname=%~nx1"
set "newname=%newname: =_%"
set "newname=%newname:)=_%"
set "newname=%newname:(=_%"
set "newname=%newname:&=_%"
set "newname=%newname:^=_%"
set "newname=%newname:$=_%"
set "newname=%newname:#=_%"
set "newname=%newname:#=_%"
set "newname=%newname:!=_%"
set "newname=%newname:-=_%"
set "newname=%newname:+=_%"
set "newname=%newname:}=_%"
set "newname=%newname:{=_%"
set "newname=%newname:]=_%"
set "newname=%newname:[=_%"
set "newname=%newname:;=_%"
set "newname=%newname:'=_%"
set "newname=%newname:`=_%"
set "newname=%newname:,=_%"
echo ren %1 "%newname%
I would recommend the rename command for this. Type ren /? at the command line for more help.

Resources