How to recursively search for a file in a remote server - cmd

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.

Related

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
}

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!

Trouble creating multiple folders from a TXT file in Windows PowerShell or CMD?

I was wondering if there is a way I could create multiple folders from a TXT document in windows PowerShell or CMD? I have a TXT file full of drawing numbers, like 5614-E-1459_SH 1 (except theirs about 500 hundred of them). Due to policies at my job I am not aloud to use third party software, so I was wondering if there was a way to do this from the command prompt or Windows PowerShell? I know that mkdir "C:\temp\5614-E-1459_SH 1" will create one of the folders I need. But is there a way to extract the files from a TXT and make it output into folders, without third party software like Text2Folders?
I have gotten this far with the PowerShell script, but as I dont have admin writes at work (where its needed most) I get the Set-ExecutionPolicy error. Is there a work around this?
$Users = Get-Content "C:\Users\usermgx\Desktop\folderDir.txt"
ForEach ($user in $users)
{
$newPath = Join-Path "C:\Users\usermgx\Desktop\Dir" -childpath $user
New-Item $newPath -type directory
}
First, you need to update your execution policy so that you can run scripts. You can do it permanently from an administrative PowerShell prompt by running:
Set-ExecutionPolicty RemoteSigned -Scope LocalMachine
If you don't have administrative rights, you can set the execution policy when you call the powershell.exe executable. From CMD:
powershell.exe -ExecutionPolicy RemoteSigned -Command C:\Path\to\your\script.ps1
Finally, you can run your script from the PowerShell ISE. Just open a new untitled document, enter your code, and hit F5, which will execute the code in script pane. I don't believe this is blocked by the execution policy.
Get-Content "C:\Users\usermgx\Desktop\folderDir.txt" |
ForEach-Object {
$dirPath = Join-Path "C:\Users\usermgx\Desktop\Dir" $_
New-Item $dirPath -ItemType Directory
}
Fortunately for what you are trying to do this is pretty easy to do with a CMD script and you won;t have to muck with the execution policy:
#echo off
for /F %%u in (C:\Users\usermgx\Desktop\folderDir.txt) DO (
mkdir "C:\Users\usermgx\Desktop\Dir\"%%u
)
If you want your powershell version to work you must chenge the execution policy as you've noted. But without admin access, you'll have to limit the scope to just yourself, like this:
set-executionpolicy -scope CurrentUser -ExecutionPolicy RemoteSigned
This is the same as #zdan but handles certain extra features like long path and filenames and spaces etc in the new foldernames.
#echo off
for /F "delims=" %%a in ('type "C:\Users\usermgx\Desktop\folderDir.txt" ') DO (
mkdir "C:\Users\usermgx\Desktop\Dir\%%a"
)
The "workaround" for the "`Set-ExecutionPolicy error'" is to set the execution policy to allow scripts to run. See http://technet.microsoft.com/en-us/library/ee176961.aspx . It's set to the most restrictive by default but any admin worth his salt will set it to something less restrictive based on what the environment requires.
Once you've done that, your script looks solid.

Batch Rename at

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

Resources