Rename files having no extension - windows

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.

Related

Find any mention of google in all files and subdirectories?

I've tried various versions of this command, hoping to get a hit. I put a blank txt document/file that only contained the string "google" and ran commands like this:
findstr /S "google" ./
findstr /S "*google*" ./
findstr /S ".google." .\
findstr /S /C:"google" c:\my\directory
What I'm thinking is I might need to have pipe an output format?
Please, tell me, in general, what am I doing wrong and how can I do this properly?
You have to mention the file in which you have to search the string.
Eg:
findstr /S "google" test_file.txt
To search in all files in a folder, add *.
Eg:
findstr /S "google" ./*
The PowerShell way for this could be:
Get-ChildItem -Path 'D:\Test' -File -Recurse |
Select-String -Pattern 'google' -SimpleMatch |
Select-Object Path, LineNumber, Line # properties you want returned
This would return
Path LineNumber Line
---- ---------- ----
D:\Test\SomeOtherTextFile.txt 6 google
D:\Test\test.txt 1 google
D:\Test\Blah\blah.txt 3 find stuff with Google
Regarding findstr I'd add /i to force case insensitivity. It shouldn't need any output redirection purely to work, it'll just output in the screen. /s will search the current directory and all sub directories. If you specify a directory it will search that directory and all sub-directories:
findstr /i /s "google" .\*
The "*" seems to be required.
findstr /i /s "google" c:\temp\*
I did some testing before posting and it seems if you don't spec the "*" it just runs forever.
Regarding the redirection of the output my first instinct was to use or ">>" to redirect stderr and append into a file. However, it doesn't seem to respect line endings! The output runs together as a single line.
With pure PowerShell, you can use Get-ChildItem with Select-String & Out-File instead.
Get-ChildItem -Recurse -File |
Select-String -pattern "google" |
Out-File c:\temp\select-String-test.txt -Append
If you are starting your search at the root of c:\ you are bound to get some bloody red access denied errors. Generally I don't care about those files, so I'll throw in -ErrorAction SilentlyContinue.

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

Copying a File to Multiple Subdirectories in Same Directory

Expanding upon this example: Copying a file to multiple folders in the same directory I want to copy an XML file to several different project directories under the same root folder, so I have tried this:
for /d %a in (C:\Root\Output\*\bin\x64\Release) do copy /y C:\Root\OtherProject\MyXml.xml %a\
Where the wildcard would be a different project name in my solution directory.
When I run this command, there is no error, but the XML is not copied, so my question is can you use wildcards like this in Windows command line or alternatively is there a better way to tackle this kind of operation from within command prompt?
Just split it:
FOR /D %1 IN (c:\root\output\*) DO (
COPY /Y c:\root\otherproject\myxml.xml %1\bin\x64\release
)
Obviously this works if all subdirectories of c:\root\output must be included (they have a bin\x64\release subdirectory.) If it's not true then you have to include an extra check:
FOR /D %1 IN (c:\root\output\*) DO (
IF EXIST "%1\bin\x64\release" (
COPY /Y c:\root\otherproject\myxml.xml "%1\bin\x64\release"
)
)
Of course you may feel this is to much code for such simple task, well then maybe it's time to switch to PowerShell, Get-ChildItem supports wildcards used the way you want:
Get-ChildItem c:\root\output\*\bin\x64\release `
| ForEach-Object -Process `
{ Copy-Item -Force c:\root\otherproject\myxml.xml $_ }
If you love short syntax:
gci c:\root\output\*\bin\x64\release | % { cp -Force c:\root\otherproject\myxml.xml $_ }

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