Batch Rename at - windows

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

Related

How to create a zip file where a file name starts with a dot

I have a situation where I am trying to create a zip file from the command line, but I am trying to add a file which starts with a dot.
powershell Compress-Archive -Path .\Dist\._test.txt -DestinationPath .\test
But this gives an error Could not find item Dist\._test.txt
How can I get this powershell command to include files named this way?
Please can you try like this, i had test the same and its working fine
powershell Compress-Archive -Path "C:\location\.test.png" -DestinationPath "C:\location\destination"
Explorer was set to show hidden items. For some reason it wasn't giving a visual indicator that this was a hidden file. I removed the checkbox for "Hidden" and it worked fine.

Windows CMD rename *.js.gz to *.js

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
}

How to recursively search for a file in a remote server

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.

PDF file not getting deleted in windows batch

I am very new to writing windows batch programmes.
I am trying to delete a pdf file in my batch programme. However the file is not getting deleted. I don't know what is wrong with this code. It works well when i try to delete a .txt file. I don't know how to trap the error also. It will be really helpful if you can guide me or redirect me to an appropriate forum.
This is the code I use:
echo Y | del \\file_path\filename.pdf
Try to do it with powershell, if you get "access dined" just run your PS console as administrator. Here is the simple code:
$myfile = "C:\Myfile.pdf"
Get-Item -Path $myfile | Remove-Item -Force
Update:
You can also use this script to remove your file from a share:
$myfile = "\\server\share\myfile.pdf"
Get-Item -Path $myfile | Remove-Item -Force
You can also run this script from a batch file:
Save the script above somewhere on your local disk eg "c:\myscript.ps1"
Create a new batch file with this command:
powershell.exe -Executionpolicy remotesigned -File c:\myscript.ps1
Where "c:\myscript.ps1" is the path to your powershell scipt

PowerShell allow to use wildcard while copying file but cmd don't allow , why?

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!

Resources