How can I write the pipe command Linux in Windows batch? - windows

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.

Related

how to do taskkill for all programs in the folder using cmd / powershell

I want to do Taskkill for all programs ending with exe in a folder with cmd / powershell
Example
taskkill /f /im C:\folder\*.exe
In PowerShell, something like this:
$files = gci "C:\Path\To\Files" -Filter "*.exe"
foreach($file in $files){
Get-Process |
Where-Object {$_.Path -eq $file.FullName} |
Stop-Process -WhatIf
}
Remove the -WhatIf when you're confident that the correct processes would be stopped.
I would advise that you try a different built-in command utility, WMIC.exe:
%SystemRoot%\System32\wbem\WMIC.exe Process Where "ExecutablePath Like 'P:\\athTo\\Folder\\%'" Call Terminate 2>NUL
Just change P:\\athTo\\Folder as needed, remembering that each backward slash requires doubling. You may have difficulties with other characters in your 'folder' name, but those are outside of the scope of my answer. To learn more about those please read, LIKE Operator
Note: If you are running the command from a batch-file, as opposed to directly within cmd, then change the % character to %%
nimizen's helpful PowerShell answer is effective, but can be simplified:
Get-Process |
Where-Object { (Split-Path -Parent $_.Path) -eq 'C:\folder' } |
Stop-Process -WhatIf
Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.
Note:
The above only targets processes whose executables are located directly in C:\folder, by checking whether the (immediate) parent path (enclosing directory) is the one of interest, using Split-Path -Parent.
If you wanted to target executables located in C:\Folder and any of its subfolders, recursively, use the following (inside Where-Object's script block ({ ... }):
$_.Path -like 'C:\folder\*'
Unlike Compo's helpful wmic.exe-based answer[1], this answer's solution also works on Unix-like platforms (using PowerShell (Core) 7+).
[1] Technically, wmic.exe is deprecated, as evidenced by wmic /? printing WMIC is deprecated in red, as the first (nonempty) line. Consider using PowerShell's CIM cmdlets, such as Get-CimInstance, instead, which has the added advantage of returning objects rather than text, for robust subsequent processing.

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
}

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!

Capturing output of a windows command to a variable

I was looking for a Windows CMD equivalent to something like
var=`ls`
in Unix. Is there a way to do so without having to iterate using for /f?
You can do it with PowerShell (and script it in a .ps1).
PS C:\> $myVar = & dir | format-table -hidetableheaders -property name
PS C:\> echo $myVar
Program Files
Program Files (x86)
Users
Windows

How to use cmd type pipe (/piping) in PowerShell?

In cmd (and bash), pipe "|" pushes output to another command in the original format of the first command's output (as string).
In PowerShell, everything that comes out the pipe is an object (even a string is a string object).
Because of that, some commands fail when run in a PowerShell command window as opposed to a Windows command window.
Example:
dir c:\windows | gzip > test.gz
When this command is run in the Windows command prompt window it works properly - directory listing of C:\windows gets compressed into test.gz file.
The same command in PowerShell fails, because PowerShell does not use cmd-style pipe and replaces it with PowerShell pipe (working with array of file system items).
Q. How do you disable the default piping behavior in PowerShell to make traditional Windows commands work identically in PowerShell?
I tried using the escape character "`" before the pipe "`|", but it didn't work. I also tried invoke-expression -command "command with | here", but it also failed.
if you want to send strings down the pipeline you can use the cmdlet "out-string"
For Example:
get-process | out-string
If you are specifically looking for a PowerShell way to zip up files, check out the PowerShell Community Extensions. there are a bunch of cmdlets to zip and unzip all kinds of files.
http://pscx.codeplex.com
If you can pipe the output of (CMD) dir into gzip, then gzip apparently knows how to parse dir output. The (string) output from the PowerShell dir command (aka Get-ChildItem) doesn't look the same, so gzip likely would not be able to parse it. But, I'd also guess that gzip would be happy to take a list of paths, so this would probably work:
dir c:\windows | select -ExpandProperty FullName | gzip > test.gz
No warrantees express or implied.
If you really need to use the old school DOS pipe system in PowerShell, it can be done by running a command in a separate, temporary DOS session:
& cmd /c "dir c:\windows | gzip > test.gz"
The /c switch tells cmd to run the command then exit. Of course, this only works if all the commands are old school DOS - you can't mix-n-match them with PowerShell commands.
While there are PowerShell alternatives to the example given in the question, there are lots of DOS programs that use the old pipe system and will not work in PowerShell. svnadmin load is one that I've the pleasure of having to deal with.
You can't. PowerShell was designed to pass objects down a pipeline, not text. There isn't a backwards-compatability mode to DOS.

Resources