I'm trying to rename all files in a folder with an extension of .js.gz to .js.
I have tried using the windows command ren like so:
ren *.js.gz *.js
This would seem like a simple thing to do through the command line but it doesn't produce the required output.
What it does end up doing is renaming exampleFile.js.gz to example.js.js
I'm sure i'd be able to accomplish this with Powershell or by using a Grunt/Gulp task.
Is there any way to get this done by using a simple built in command?
At the DOS command line I typed: ren ".js.gz" "."
For me this command dropped the '.gz' file extension. I would take backups of your files before experimenting with them, DOS can sometimes produce unexpected results. I'm using Microsoft Windows [Version 10.0.16299.665].
The handling of extensions can be tricky. This code uses a regex to create the new filename. When you are confident that the files will be renamed correctly, remove the -WhatIf from the Move-Item cmdlet.
Get-ChildItem -File -Filter '*.js.gz' |
ForEach-Object {
Move-Item -Path $_.FullName -Destination $($_.FullName -replace '^(.*)\.js\.gz$', '$1.js') -WhatIf
}
Related
I'm building a solution that needs to execute a dynamically built terminal command cross-platform. This is the command on macOS (actually a single line of text, changed for readability):
cat "/path/to/file1.txt" "/path/to/file2.txt" > "/path/to/output.txt" ;
rm "/path/to/file1.txt" ; rm "/path/to/file2.txt"
After a bit of research, the equivalent on Windows would be:
cmd.exe /c type "C:/path/to/file1.txt" "C:/path/to/file2.txt" > "C:/path/to/output.txt" ;
del "C:/path/to/file1.txt" ; del "C:/path/to/file2.txt"
Now, that seems to work when I put it in manually in the PowerShell, but I get errors. Note that the concatenation and deletion of the files appears to work, but I'm getting the following error:
The system cannot find the file specified.
Error occurred while processing C:/path/to/file1.txt
The system cannot find the file specified.
Error occurred while processing C:/path/to/file2.txt
When the Windows version of the command is dynamically built and executed, the concatenation works but the file removal does not, and my guess is it's because of these errors.
What does the Windows version of this need to be in order to work exactly like the macOS version?
(In case you're wondering, this is within a FileMaker database that uses the BE_ExecuteSystemCommand function from the BaseElements plugin.)
Windows uses backslashes, not forward slashes, in path names. Some commands will allow you to use forward slashes instead, but del is not one of them:
C:\Users\UoW>del "c:/Users/UoW/test.dat"
The system cannot find the path specified.
C:\Users\UoW>del "c:\Users\UoW\test.dat"
C:\Users\UoW>
Try staying in PowerShell, try this:
Add-Content -Path "C:/path/to/output.txt" -Value (Get-Content "C:/path/to/file1.txt")
Add-Content -Path "C:/path/to/output.txt" -Value (Get-Content "C:/path/to/file2.txt")
Remove-Item -Path "C:/path/to/file1.txt"
Remove-Item -Path "C:/path/to/file2.txt"
I am using dir \\pwdf1280\rep\"*.txt" /S to search for all the txt files in the folder rep. I used the /S argument to search for the same in the sub directories, but the problem is the command runs forever. I am basically writing a perl script which has to find these files along with their timestamp.
Is there any other approach to solve this, or can I improve on the above command?
The problem is the dir command I mentioned above runs forever and does not display files present in rep's sub directories. It just displays files present in rep directory. I want to search for a file in the rep's sub directories.
One way that might speed the process up is to run the directory search on the remote machine using PowerShell.
C:\src\powershell> type .\rc001.ps1
Invoke-Command `
-ComputerName 'pwdf1280' `
-Command { Get-ChildItem '\\pwdf1280\rep' -Recurse -Filter '*.txt' } |
ForEach-Object { $_.FullName }
Then, from a cmd script run:
C:\src\powershell> powershell -NoProfile -File .\rc001.ps1
Alternatives might be to run the dir command remotely using psexec from SysInternals or plink from PuTTY.
Previously i was using cmd to copy file (abc.txt) from C:\vackwrk\24may\abc.txt to G:\work\ and command i used in cmd was copy C:\vackwrk\*\abc.txt G:\work\ it didn't work but when i use PowerShell it works (file copied to another folder) PS C:\Windows\system32> copy C:\vackwrk\*\abc.txt G:\work\ (Wildcard (*) in the path don't work while copying from cmd)
So, question is - Why cmd don't allow to use wildcard but PowerShell allow ? Is PowerShell better than cmd ?
copy at the command prompt and copy in PowerShell are completely different.
copy is an internal command implemented by cmd.exe itself. It isn't an executable that you can run separately. You can see the help if you run copy /?.
copy in PowerShell in an alias to the Copy-Item cmdlet. You can see the help by using help copy.
The two are superficially similar but actually have some different features. As you've seen, PowerShell will expand any wildcards in the path, not just wildcards in file names. Another difference is that copy in cmd allows you to concatenate files with +, so copy file1.txt+file2.txt+fil3.txt out.txt concatenates the files and puts the output in out.txt. There is no similar feature in Copy-Item. PowerShell's Copy-Item cmdlet is more similar to xcopy that copy.
It depends on what you want to use it for. But It's a fact that PowerShell is more "powerful" than CMD.
Powershell is better than CMD, its supposed to be it's the CMD's future.
You can't use wildcard in CMD, but if you found a way to do that using powershell, you're good! my advice:
get-childItem -path /path/ -recursive -inculde "abc.txt" -Exclude "/whatever you dont need" | foreach{
copy-item -path /path/ -destination "/destination/";
}
good luck!
I need help with a Command Prompt Script with regards to Bulk Renaming AVI & JPG files within their folders by using their respective "Folder Names", however the JPG file named "folder" must remain unchanged.
Example Before:
C:\Temp\Videos\Terminator (1984ST)\Terminator (1984).avi
C:\Temp\Videos\Terminator (1984ST)\Terminator (1984).jpg
C:\Temp\Videos\Terminator\Folder.jpg
Example After:
C:\Temp\Videos\Terminator (1984ST)\Terminator (1984ST).avi
C:\Temp\Videos\Terminator (1984ST)\Terminator (1984ST).jpg
C:\Temp\Videos\Terminator\Folder.jpg
Thanks in advance
Try with Windows PowerShell: see here how to open Windows PowerShell.
And try these commands line:
cd C:\Temp\Videos\"Terminator (1984ST)"
dir * | rename-item -NewName {$_.name -replace "1984","1984ST"}
Reference: PowerShell, commands
How can I write the | (Linux) command in a Windows cmd (batch file)?
I don't know how to write this little Linux script in Windows:
find -r * |grep *.fd | open
In Windows:
dir /S ??? open
I don't really know what open does. If it simply starts an associated application with the respective file, then the following should do it:
for /r %f in (*.fd) do (start "" "%f")
In PowerShell you can do the same with:
Get-ChildItem -Recurse -Filter *.fd | Invoke-Item
or shorter:
gci -rec -fi *.fd | ii
The regular command shell in windows is lacking in power and features. However, Windows Power Shell has the ability to run a lot of ninja commands similar to *nix shells.
You can get more information about power shell on MSDN - http://msdn.microsoft.com/en-us/library/aa973757%28v=vs.85%29.aspx
Here is an example I googled from Powershell help itself:
-------------------------- EXAMPLE 4 --------------------------
C:\PS>get-childitem
c:\windows\system32* -include *.txt
-recurse | select-string -pattern "Microsoft" -casesensitive
This command examines all files in the
subdirectories of C:\Windows\System32
with the .txt file extension, for the
string "Microsoft". The CaseSensitive
parameter indicates that the 'M' in
'Microsoft' must be capitalized and
the rest of the characters must be
lowercase for a match to occur.