web-dev-qa-db-ja.com

Windows Powershellを介して新しいファイルを作成する

以下の質問をグーグル検索しましたが、回答が見つかりませんでした。誰かがこれについて私を助けることができますか? Windows Powershellを介して新しいファイルを作成するコマンドは何ですか?

37
JR Sahoo.'JS'

エコーを使用してファイルを作成するには

echo some-text  > filename.txt

例:

C:\>echo This is a sample text file > sample.txt
C:\>type sample.txt
This is a sample text file
C:\>

Fsutilを使用してファイルを作成するには

fsutil file createnew filename number_of_bytes

例:

fsutil file createnew sample2.txt 2000
File C:\sample2.txt is created
C:\data>dir
01/23/2016  09:34 PM     2,000 sample2.txt
C:\data>

制限事項

Fsutilは管理者のみが使用できます。管理者以外のユーザーの場合、エラーが発生します。

c:\>fsutil file /?

FSUTILユーティリティを使用するには、管理者権限が必要です。 c:>

お役に立てれば!

34
Jože Strožer

あなたはテキストファイルを作成しようとしていると思いますか?

New-Item c:\scripts\new_file.txt -type file

「C:\ scripts\new_file.txt」は、ファイル名と拡張子を含む完全修飾パスです。

TechNet記事 から取得

50
J. D.

ストリートスマート(クイック、ダーティですが機能します):(ファイルを変更し、コンパイラが失敗する可能性のある非表示の文字を追加する可能性があります)

$null > file.txt
$null > file.html

教科書による方法:

New-Item -path <path to the destination file> -type file

例:

New-Item -path "c:\" -type file -name "somefile.txt"

OR

ni file.xt -type file

-pathパラメータがない場合は、現在の作業ディレクトリに作成されます

14
Gajendra D Ambi

これは、Powershellで空のテキストファイルを作成する別の方法です。これにより、エンコーディングを指定できます。

最初の例

空のテキストファイルの場合:

Out-File C:\filename.txt -encoding ascii

-encoding asciiがない場合、PowershellはデフォルトでUnicodeになります。別のソースから読み取り可能または編集可能にする場合は、asciiを指定する必要があります。

新しいテキストでファイルを上書きする:

"Some Text on first line" | Out-File C:\filename1.txt -encoding ascii

これは、filename.txt内のテキストをSome Text on first line.に置き換えます

現在のファイルの内容にテキストを追加:

"Some More Text after the old text" | Out-File C:\filename1.txt -encoding ascii -Append

-Appendを指定すると、filename.txtの現在のコンテンツがそのまま残り、Some More Text after the old textがファイルの末尾に追加され、現在のコンテンツはそのまま残ります。

7
Josh H
ni filename.txt

置換filename.txtあなたのファイルで。

これが質問に対する最も簡単な答えだと思いました。詳細については、他の答えを参照してください。

6
themefield
                                                       # encodings:

New-Item file.js -ItemType File -Value "some content"  # UTF-8

"some content" | Out-File main.js -Encoding utf8       # UTF-8-BOM

echo "some content" > file.js                          # UCS-2 LE BOM
0
user2263572